
leetcode
ccc_yxc
这个作者很懒,什么都没留下…
展开
-
刷算法题 前言
前言好久没写博客的。日常笔记放在印象笔记,而且自己刚刚出来工作,处于汲取知识的阶段,对知识也没有太多自己的经验见解,写的博客也少了。另一方面当然也是自己懒癌晚期了。发现自己的数据结构和算法,真的菜得不行。看到算法题,好像有思路,但是就是做不出来。一方面是自己的基础不够坚实,具体到写代码时因为一些小问题碍手碍脚。另一方面是目前能力问题,只能想清楚一部分,再深写下去,就迷糊了。类比就是高三时做...原创 2018-03-25 11:48:56 · 309 阅读 · 0 评论 -
LeetCode 111. Minimum Depth of Binary Tree
111. Minimum Depth of Binary Tree1.条件不能是root == NULL,而是判断叶子结点的条件。class MinDepth {public: int minDepth(TreeNode* root) { if (root == NULL) { return 0; } ...原创 2018-06-07 17:11:44 · 224 阅读 · 0 评论 -
Leetcode 112. Path Sum
112. Path Sum1.有点回溯的感觉,试完左树试右树。 2.注意叶子节点的判断条件 3.root=NULL,sum=0时也要返回false。class HasPathSum {public: bool hasPathSum(TreeNode* root, int sum) { if (root == NULL) { ...原创 2018-06-07 17:38:39 · 157 阅读 · 0 评论 -
LeetCode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock IIclass MaxProfit {public: int maxProfit(vector<int>& prices) { if (prices.empty()) { return 0; } in...原创 2018-06-07 19:27:17 · 152 阅读 · 0 评论 -
LeetCode 55. Jump Game
55. Jump Game水平有限,这题没有意识到是贪心的题,用递归和动态规划分别写了一遍。前者报超时,后者报内存限制。正确代码 bool canJump(vector<int>& nums) { if (nums.empty()) { return false; } // tan...原创 2018-06-23 21:26:54 · 328 阅读 · 0 评论 -
LeetCoden 62. Unique Paths
62. Unique Paths递归解法超时,需要用动态规划动态规划解法 int uniquePaths(int m, int n) { if (m <= 0 || n <= 0) { return -1; } vector<vector<int>> res...原创 2018-06-25 00:15:08 · 267 阅读 · 0 评论 -
LeetCode 168. Excel Sheet Column Title
168. Excel Sheet Column Title这题。。做了好久。脑子没转过来 1.以为是27进制,不知道怎么想的。。。 2.进制从0开始和从1开始。。。class ConvertToTitle {public: string convertToTitle(int n) { if (n < 1) { ...原创 2018-06-10 11:02:58 · 134 阅读 · 0 评论 -
LeetCode 172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes主要看5的个数就可以了。class TrailingZeroes {public: int trailingZeroes(int n) { if (n < 5) { return 0; } int count = 0; ...原创 2018-06-10 11:50:16 · 154 阅读 · 0 评论 -
LeetCode 190. Reverse Bits
190. Reverse Bits二进制操作,一直是自己比较薄弱的地方。 解法一:对每个二进制位进行操作class ReverseBits {public: uint32_t reverseBits(uint32_t n) { uint32_t movement = n; uint32_t res = 0; int mask ...原创 2018-06-10 17:10:42 · 174 阅读 · 0 评论 -
LeetCode 204. Count Primes
204. Count Primes主要是判断整数是否为质数,判断有如下三种,效率依次提高 bool isPrimes(int n) { bool bIsPrimes = true; for (int i = 2; i <= n; i++) { if (n % i == 0) ...原创 2018-06-10 17:46:17 · 199 阅读 · 0 评论 -
LeetCode 205. Isomorphic Strings
205. Isomorphic Strings题目要求的是双向关系,一开始理解错了class IsIsomorphic {public: bool isIsomorphic(string s, string t) { if (s.size() != t.size()) { return false; }...原创 2018-06-10 21:42:47 · 147 阅读 · 0 评论 -
Leetcode 110. Balanced Binary Tree
110. Balanced Binary Tree1.返回bool是为了返回判断的结果,判断当前的结点是否平衡 2.depth是为了记录高度class IsBalanced {public: bool isBalanced(TreeNode* root) { if (root == NULL) { return true;...原创 2018-06-07 16:48:10 · 164 阅读 · 0 评论 -
Leetcode 67.AddBinary
67.AddBinary跟66.Plus One 差不多,我还是先把他翻转过来。 因为有可能两个字符串长度不一样。短的字符串如果超出长度,循环时一直加0就好了。class AddBinary {public: string addBinary(string a, string b) { reverse(a.begin(), a.end()); ...原创 2018-06-05 13:18:16 · 158 阅读 · 0 评论 -
leetcode 11.盛最多水的容器
11. 盛最多水的容器双指针法,挺常用的,学习一下。class MaxArea {public: int maxArea(vector<int>& height) { if (height.size() < 2) { return 0; } int nLeft = 0...原创 2018-03-25 18:31:57 · 1547 阅读 · 0 评论 -
leetcode 15. 三数之和
15. 三数之和方法网上有很多,这里主要分析一下用set排重的问题。 先说知识点: 1.set采用等价比较,而不是等值(Effective STL)。就是说不用operator==,只用operator<。 2.set只实现了内置类型的比较函数。 3.当set里面元素相等时,总是返回false。 4.set比较函数的实现方法如下struct A{ int a;}...原创 2018-03-25 23:09:51 · 752 阅读 · 0 评论 -
leetcode 29.Divide Two Integers
29.Divide Two Integers这道题慢慢减不行,需要位移运算实现循环一次减多个的效果。 下面代码有个坑是,当 count = 31时,1需要左移31次,编程INT_MIN。class Divide {public: int divide(int dividend, int divisor) { // x / % if (diviso...原创 2018-03-28 00:29:15 · 179 阅读 · 0 评论 -
LeetCode 257. Binary Tree Paths
257. Binary Tree Paths1.这道题,一开始使用find_last_of的时候,想要匹配 ‘-‘,但是val可以是负数,所以匹配错误。 2.整型转化为字符串的方法:利用stringstreamstringstream ss;ss << root->val;string rval = ss.str();class BinaryTreePaths ...原创 2018-06-11 11:26:52 · 209 阅读 · 0 评论 -
LeetCode 292. Nim Game
292. Nim Game感觉这题还是很有意义的。让我意识到,算法题,其实核心还是数学思想。 有时候做算法题,还需要有抽象能力,转化能力。解法理解: 题意理解到,只要最终剩下4个,而且轮到对面先抽的轮次,那么等对方抽完,自己再抽剩下的,就赢了。 所以只要第一次把4的余数数量石头抽走,接下来,对方无论抽几个,我们就补几个凑成4。这样就保证了在最终的轮次会剩下4个,而且是对方先抽。...原创 2018-06-11 16:13:05 · 178 阅读 · 0 评论 -
LeetCode 31. Next Permutation
31. Next Permutation这题不会做。。。 找全排列的下一个序列 最笨的方法是把全排列求出来,然后排个序,再得出下一个全排列。 但是空间不允许。这题也提供了一个直接找全排列下一个序列的方法 1.从后往前遍历数组,找出非递增的那个下标x 2.如果找不到,就是全排列的第一个。 3.找到后,与A数替换 A数是指(x + 1)–>nums.end()这段下标中,比...原创 2018-06-11 21:09:46 · 195 阅读 · 0 评论 -
leetcode 35.Search Insert Position
考查二分查找class SearchInsert {public: int searchInsert(vector<int>& nums, int target) { if (nums.empty()) { return 0; } int nLeft = 0; ...原创 2018-06-05 11:39:55 · 106 阅读 · 0 评论 -
Leetcode 38.Count and Say
38.Count and Say 这题理解了题意就会比较好做。 1 11 21 1211 111221 规定第一项是 1 2时,读第一项,就是 1个1 3时,读第二项,就是 2个1 4时,读第三项,就是 1个2,1个1 …要把相同数字的归纳为数量表示出来。class CountAndSay {public: string coun...原创 2018-06-05 12:08:49 · 149 阅读 · 0 评论 -
LeetCode 107.Binary Tree Level Order Traversal ii
107.Binary Tree Level Order Traversal ii拆分任务: 1.层次遍历 2.翻转vectorclass LevelOrderBottom {public: vector<vector<int>> levelOrderBottom(TreeNode* root) { if (root == NULL) ...原创 2018-06-05 16:55:55 · 204 阅读 · 0 评论 -
Leetcode 66.Plus One
66.Plus One我采用的方法是先把整个vector翻转,再顺序加。最后再重新翻转回来。class PlusOne {public: vector<int> plusOne(vector<int>& digits) { vector<int> tmpDig = digits; reverse(tm...原创 2018-06-05 12:30:25 · 165 阅读 · 0 评论 -
LeetCode 290. Word Pattern
290. Word Patternclass WordPattern {public: bool wordPattern(string pattern, string str) { if (pattern.empty() && str.empty()) { return true; } ...原创 2018-06-11 10:32:07 · 184 阅读 · 0 评论