![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
笨笨的小棒棒
这个作者很懒,什么都没留下…
展开
-
刷leetcode常用小技巧
在使用bfs进行遍历时一般为了上下左右四个方向时会使用dx={0,1,0,-1},dy={-1,0,1,0};为了简单可以写为dxy={1,0,-1,0,1} x=i+dxy[j],y=j+dxy[j+1]同理八个方向可以写为dxy={1,1 ,-1,-1,1 ,0 ,-1 ,0 ,1}...原创 2020-03-04 16:59:08 · 772 阅读 · 0 评论 -
leetcode 53:最大子序和
方法1:暴力解决方式int maxSubArray(std::vector<int>&nums){ int max=nums[0]; int sum; for(int i=0;i<nums.size();i++){ for(int j=i;j<nums.size();j++){ sum=0; ...原创 2018-09-26 09:34:09 · 189 阅读 · 0 评论 -
leetcode 108:将有序数组转换为二叉搜索树
首先要比较清楚搜索树的概念,搜索树就是左子树上所有的节点均小于当前节点,而右子树所有的节点均大于该节点,因为数组是有序的,只需要根据每次寻找处于中间的下标即可保证左子树和右子树的条件满足。另外要清楚平衡二叉树的概念,每个节点的左子树和右子树的高度差的绝对值不超过1,清楚这些概念我们就会发现用分治的方式很容易解决该问题。TreeNode*ArrayToBST(std::vector<...原创 2018-10-11 09:15:00 · 104 阅读 · 0 评论 -
leetcode 58: 最后一个单词的长度
这个题还是很简单的,直接从后开始计数即可int lengthOfLastWord(std::string s) { if(s.size()==0) return 0; int c=0,flag=0; int i=s.size()-1; for(int i=s.size()-1;i>=0;i--){ if(s[i]!=...原创 2018-09-26 21:32:56 · 87 阅读 · 0 评论 -
leetcode 66: 加一
没有想到好的方法,只好使用比较笨的方式来做了std::vector<int> plusOne(std::vector<int> &digits){ std::vector<int> b; int len=digits.size()-1; for(int i=len;i>=0;i--){ if(dig...原创 2018-09-26 22:20:25 · 82 阅读 · 0 评论 -
leetcode 67:二进制求和
std::string addBinary(std::string a, std::string b) { std::string s1=a.size()>=b.size()?a:b; std::string s2=a.size()<b.size()?a:b; int max=std::max(a.size(),b.size()); int min...原创 2018-09-26 23:38:25 · 81 阅读 · 0 评论 -
leetcode 69: x 的平方根
int mySqrt(int x) { if(x==0) return 0; if(x==1||x==2||x==3) return 1; for(int i=2;i<=x/2;i++){ if(x/i==i) return i; if((x/i>i)&...原创 2018-09-27 00:02:26 · 98 阅读 · 0 评论 -
leetcode 83:删除排序链表中的重复元素
ListNode *deleteDuplications(ListNode* head){ if(head==NULL)return head; ListNode *l1=new ListNode(0); ListNode *l2=l1; l1->next=new ListNode(head->val); head=head->next; ...原创 2018-09-27 08:50:37 · 112 阅读 · 0 评论 -
leetcode 88:合并两个有序数组
void merge(std::vector<int> &nums1,int m,std::vector<int>&nums2,int n){ int i=0,j=0; int c=m; while(i!=n&&j!=c){ if(nums1[j]<=nums2[i])j++; els...原创 2018-09-27 21:22:06 · 245 阅读 · 0 评论 -
leetcode 100:相同的树
根据题意可知,需要结构和值相等才行,使用递归的方式较为简单bool isSameTree(TreeNode*p,TreeNode*q){ if(p==NULL&&q==NULL) return true; if((p==NULL&&q!=NULL)||(p!=NULL&&q==NULL)) r...原创 2018-09-27 21:48:44 · 77 阅读 · 0 评论 -
leetcode 101:对称二叉树
这个题明知道用递归,但是乍一看想不到怎么才能用到递归,但是后来会发现其实除了根节点之后是两个二叉树,比较两个二叉树,记为l1和l2,往下的比较就会发现,除了比较l1和l2的根节点是否相等之外,其他的是比较l1的左节点是否等于l2的右节点,l1的右节点是否等于l2的左节点,所以此题就可以在100题的基础上稍加修改bool isEqual(TreeNode*l1,TreeNode *l2){...原创 2018-09-27 22:40:39 · 128 阅读 · 0 评论 -
leetcode 104:二叉树的最大深度
这个题相对简单 简单递归即可int maxDepth(TreeNode* root){ if(root==NULL) return 0; else return std::max(maxDepth(root->left)+1,maxDepth(root->right)+1);} ...原创 2018-09-27 22:50:24 · 126 阅读 · 0 评论 -
leetcode 107: 二叉树的层次遍历 II
对于二叉树来说,递归是最好用的方式void levelPush(TreeNode*l1,int level,std::vector<std::vector<int>>&a){ if(l1==NULL)return; if(level==a.size()){ std::vector<int> b; ...原创 2018-09-28 09:52:20 · 109 阅读 · 0 评论 -
leetcode 102:二叉树的层次遍历
一:队列std::vector<std::vector<int>> levelOrder(TreeNode*root){ std::vector<std::vector<int>> a; if(root==NULL)return a; std::deque<TreeNode*> q; std::...原创 2018-09-28 09:59:39 · 179 阅读 · 0 评论 -
leetcode 125:验证回文串
bool isPalindrome(std::string s){ std::vector<char> a; for(int i=0;i<s.size();i++){ if((s[i]>='a'&&s[i]<='z')||(s[i]>='0'&&s[i]<='9')) a.push_back(s[i]);...原创 2018-10-12 22:26:49 · 100 阅读 · 0 评论 -
leetcode 23:合并K个排序链表
使用分治算法很容易解决 ListNode *merge(std::vector<ListNode*>& lists,int s,int t){ if(s==t)return lists[s]; int m=(s+t)/2; ListNode*l=merge(lists,s,m); ListNode*r=merge(lists,m+1,t...原创 2018-10-12 23:12:26 · 128 阅读 · 0 评论 -
leetcode 264:丑数 II
我们需要做的是找到2乘某个数 或3乘某个数 或5乘某个数中的最小数刚好大于末尾的数字。t2,t3,t5分别表示乘以2 乘以3 乘以5最小结果的下标,每次找三者之中的最小值,相应的t加一 如果有多个最小值,多个相应的t均加1int nthUglyNumber(int n) { std::vector<int> a; if(n<7)return n;...原创 2018-10-13 09:23:25 · 277 阅读 · 0 评论 -
leetcode 263:丑数
bool isUgly(int num) { if(num<=0)return false; std::vector<int> a; if(num<7)return true; while(num!=1){ if(num%5==0){ num=num/5; } el...原创 2018-10-13 09:33:09 · 126 阅读 · 0 评论 -
leetcode 204:计数质数
int countPrimes(int n) { if(n<=0)return 0; int c=0; for(int i=2;i<n;i++){ int flag=0; for(int j=2;j<=std::sqrt(i);j++){ if(i%j==0){ ...原创 2018-10-13 09:43:26 · 100 阅读 · 0 评论 -
leetcode 3:无重复字符的最长子串
使用队列来实现int lengthOfLongestSubstring(std::string s) { std::deque<char> a; if(s.size()==0) return 0; int d=0; int max=0; int flag=0; for(int i=0;i<s.size();...原创 2018-10-13 22:55:41 · 87 阅读 · 0 评论 -
leetcode 4:两个排序数组的中位数
寻找中位数,当m+n是奇数时,中位数为第(m+n+1)/2个数,当m+n为偶数时,中位数为第(m+n+1)/2和(m+n+2)/2的平均数根据整数的取整,我们可以统一为中位数为第(m+n+1)/2和(m+n+2)/2的平均数。如何使用二分法求两个有序数组的第k个数首先将数组A和数组B分为left_A(下标为0~k/2-1),right_A,left_B(下标为0~k/2-1),ri...原创 2018-10-14 12:09:23 · 151 阅读 · 0 评论 -
leetcode 121:买卖股票的最佳时机
这个题相对简单方法一:直接使用暴力解决方式即可解决,时间复杂度是O(n*n)int maxProfit(std::vector<int>&prices){ int len=prices.size(); int max=0; for(int i=0;i<len-1;i++){ for(int j=i+1;j<len...原创 2018-10-07 21:36:37 · 127 阅读 · 0 评论 -
leetcode 122:买卖股票的最佳时机 II
本题有个很明显的规律,但是我也是看了很久才明白的,就是当第i个值不是最后一个值时,当它大于第i-1个值,并且大于等于第i+1个值时一定是需要分段的,而对于最后一个元素,只要前面有比它小的数肯定也是需要分段的,我的表述不太清楚,看代码的话可能更容易理解。int maxProfit(std::vector<int>& prices) { int len=price...原创 2018-10-07 23:20:16 · 83 阅读 · 0 评论 -
leetcode 123: 买卖股票的最佳时机 III
本题跟122类似,但是更加绕了一点儿,122题可以看做多个分段求和的结果,而对于本题来说如果是递增或递减的数列,只需求整个数列最大值结果,而对于存在第i个数值大于第i-1并大于第i+1个值的数列,分成两个数列求两个数列的最大值,两个数列的最大值相加得到最大利润int maxP(std::vector<int> &a,int s,int t){ if(s==t)...原创 2018-10-08 09:21:44 · 132 阅读 · 0 评论 -
leetcode 111:二叉树的最小深度
本题也相对简单,只需要简单迭代即可int minDepth(TreeNode*root){ if(root==NULL) return 0; TreeNode*l=root->left; TreeNode*r=root->right; if(l==NULL&&r==NULL) return 1;...原创 2018-09-29 22:00:58 · 159 阅读 · 0 评论 -
leetcode 112:路径总和
感觉二叉树用递归基本都能做出来,这个也比较简单bool hasPathSum(TreeNode* root,int sum){ if(root==NULL) return false; TreeNode*l=root->left; TreeNode*r=root->right; if(l==NULL&&r==NU...原创 2018-09-29 22:30:51 · 117 阅读 · 0 评论 -
leetcode 118: 杨辉三角
自己没有想到什么好的方式,就用了比较笨的方式std::vector<std::vector<int>> generate(int numRows) { std::vector<std::vector<int>>a; std::vector<int> v; std::vector<int> w;...原创 2018-09-29 23:09:05 · 109 阅读 · 0 评论 -
leetcode 119:杨辉三角 II
方法一:简单的方式,空间复杂度不是O(k)std::vector<int> getRow(int rowIndex) { std::vector<std::vector<int>> a; std::vector<int> v; v.push_back(1); a.push_back(v); v.pus...原创 2018-10-08 22:08:39 · 335 阅读 · 0 评论 -
leetcode 110:平衡二叉树
直接选择用递归就可以了,因为需要求高度,所以需要求深度。int maxDepth(TreeNode*root){ if(root==NULL)return 0; TreeNode*l=root->left; TreeNode*r=root->right; return 1+std::max(maxDepth(l),maxDepth(r));}...原创 2018-10-08 22:40:27 · 132 阅读 · 0 评论 -
leetcode 617:合并二叉树
直接递归即可TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { TreeNode *t3; TreeNode *l1,*r1; TreeNode *l2,*r2; if(t1!=NULL&&t2!=NULL){ t3=new TreeNode(t1->val+t2-&g...原创 2018-10-09 08:49:40 · 106 阅读 · 0 评论 -
leetcode 103:二叉树的锯齿形层次遍历
二叉树的问题,这个题我选择使用队列来做,两个队列,可能方法比较笨std::vector<std::vector<int>> zigzagLevelOrder(TreeNode* root) { std::vector<std::vector<int>>a; std::deque<TreeNode*>q1; ...原创 2018-10-09 21:57:10 · 138 阅读 · 0 评论 -
leetcode 94: 二叉树的中序遍历
方法一:使用递归的方式 比较简单std::vector<int>a;std::vector<int> inorderTraversal(TreeNode* root) { if(root){ inorderTraversal(root->left); a.push_back(root->val); ...原创 2018-10-09 23:21:14 · 89 阅读 · 0 评论 -
leetcode 98: 验证二叉搜索树
这个题有些难度,主要是需要注意节点左侧所有的节点小于该节点,右侧的均大于该节点,首先先简单的判断该节点的左节点和右节点是否满足条件,之后需要查看左子树中所有的右节点是否都小于该节点,右子数上所有的左节点是否小于该节点,对每个节点执行该操作。bool isLess(TreeNode*root,int a){//判断是否所有的左子树的右节点小于该节点 if(root==NULL)re...原创 2018-10-10 22:38:31 · 118 阅读 · 0 评论 -
leetcode 11: 盛最多水的容器
容器盛的水是所x轴的差值×较小的高int maxArea(std::vector<int>& height) { int max=0; int h=0; for(int i=0;i<height.size()-1;i++) { for(int j=i+1;j<height.size();j++) ...原创 2018-10-22 22:42:25 · 127 阅读 · 0 评论 -
leetcode 12:整数转罗马数字
string intToRoman(int num) { vector<char> a; int m=num/1000; for(int i=0;i<m;i++){ a.push_back('M'); } num=num%1000; if(num/900){ a.push_back('C')...原创 2018-10-22 22:44:50 · 119 阅读 · 0 评论 -
leetcode 13:罗马数字转整数
int romanToInt(string s) { int a=0; for(int i=0;i<s.size();){ if(s[i]=='M'){ a=a+1000; i++; } else if(s[i]==...原创 2018-10-22 22:46:44 · 157 阅读 · 0 评论 -
leetcode 14:最长公共前缀
string longestCommonPrefix(vector<string>& strs) { if(strs.size()==0){ return ""; } string aa=strs[0]; int flag=0; int bb=0; for(int j=0;j<aa.size();j++...原创 2018-10-22 22:48:52 · 121 阅读 · 0 评论 -
leetcode 15:三数之和
vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> a; vector<int> b; sort(nums.begin(),nums.end()); if(nums.size()<3)原创 2018-10-22 22:50:03 · 154 阅读 · 0 评论 -
leetcode 16:最接近的三数之和
跟leetcode 15类似int threeSumClosest(std::vector<int>& nums, int target) { std::vector<std::vector<int>> a; std::vector<int> b; sort(nums.begin(),nums.end());...原创 2018-10-23 00:05:47 · 103 阅读 · 0 评论 -
leetcode 49: 字母异位词分组
std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string> &strs){ std::vector<std::vector<std::string>> sss; std::map<std::string,int...原创 2018-10-25 09:07:25 · 128 阅读 · 0 评论