├── 191. number-of-1-bits ├── main.py ├── main.cs ├── main_1.cpp ├── main.java ├── main.cpp └── README.md ├── 258. add-digits ├── main.cpp └── README.md ├── 50. powx-n ├── main.cpp ├── main_2.cpp ├── main_1.cpp └── README.md ├── 224. basic-calculator ├── main.js ├── README.md └── main.cpp ├── 10. regular-expression-matching ├── main.cpp ├── main_1.cpp └── README.md ├── 28. implement-strstr ├── main.cpp ├── README.md └── main_1.cpp ├── 1512. number-of-good-pairs ├── README.md └── main.cpp ├── 263. ugly-number ├── README.md └── main.cpp ├── 206. reverse-linked-list ├── README.md └── main.cpp ├── 141. linked-list-cycle ├── README.md ├── main.cpp └── main_1.cpp ├── 234. palindrome-linked-list ├── README.md └── main.cpp ├── 58. length-of-last-word ├── README.md └── main.cpp ├── 59. spiral-matrix-ii ├── README.md └── main.cpp ├── 190. reverse-bits ├── main.py ├── README.md └── main.cpp ├── LCP 01. guess-numbers ├── README.md └── main.cpp ├── 剑指 Offer 24. fan-zhuan-lian-biao-lcof ├── README.md └── main.cpp ├── 283. move-zeroes ├── README.md └── main.cpp ├── 剑指 Offer 58 - II. zuo-xuan-zhuan-zi-fu-chuan-lcof ├── main.cpp └── README.md ├── 136. single-number ├── main_1.cpp ├── main.cpp └── README.md ├── 1822. sign-of-the-product-of-an-array ├── README.md └── main.cpp ├── 88. merge-sorted-array ├── README.md └── main.cpp ├── 1006. clumsy-factorial ├── README.md └── main.cpp ├── 173. binary-search-tree-iterator ├── README.md └── main.cpp ├── 27. remove-element ├── main.cpp └── README.md ├── 29. divide-two-integers ├── main.cpp └── README.md ├── 70. climbing-stairs ├── README.md └── main.cpp ├── 8. string-to-integer-atoi ├── main.cpp ├── README.md └── main_1.cpp ├── 169. majority-element ├── README.md └── main.cpp ├── 1480. running-sum-of-1d-array ├── main_1.cpp ├── README.md └── main.cpp ├── 21. merge-two-sorted-lists ├── README.md └── main.cpp ├── 303. range-sum-query-immutable ├── README.md └── main.cpp ├── 80. remove-duplicates-from-sorted-array-ii ├── README.md └── main.cpp ├── 18. 4sum ├── README.md └── main.cpp ├── 4. median-of-two-sorted-arrays ├── README.md └── main.cpp ├── 74. search-a-2d-matrix ├── README.md ├── main_1.cpp └── main.cpp ├── 1431. kids-with-the-greatest-number-of-candies ├── README.md └── main.cpp ├── 面试题 17.21. volume-of-histogram-lcci ├── README.md └── main.cpp ├── 1672. richest-customer-wealth ├── README.md └── main.cpp ├── 155. min-stack ├── README.md ├── main.py ├── main_1.cpp └── main.cpp ├── 面试题 02.03. delete-middle-node-lcci ├── README.md └── main.cpp ├── 35. search-insert-position ├── README.md └── main.cpp ├── 17. letter-combinations-of-a-phone-number ├── README.md └── main.cpp ├── 6. zigzag-conversion ├── README.md └── main.cpp ├── 1603. design-parking-system ├── README.md └── main.cpp ├── 227. basic-calculator-ii ├── README.md └── main.cpp ├── 341. flatten-nested-list-iterator ├── README.md └── main.cpp ├── 61. rotate-list ├── README.md └── main.cpp ├── 83. remove-duplicates-from-sorted-list ├── README.md └── main.cpp ├── 1047. remove-all-adjacent-duplicates-in-string ├── README.md └── main.cpp ├── 20. valid-parentheses ├── README.md └── main.cpp ├── 338. counting-bits ├── README.md ├── main_1.cpp └── main.cpp ├── 53. maximum-subarray ├── main.cpp └── README.md ├── 23. merge-k-sorted-lists ├── README.md └── main.cpp ├── 101. symmetric-tree ├── README.md └── main.cpp ├── 26. remove-duplicates-from-sorted-array ├── main_1.cpp ├── README.md └── main.cpp ├── 503. next-greater-element-ii ├── README.md └── main.cpp ├── 232. implement-queue-using-stacks ├── README.md └── main.cpp ├── 208. implement-trie-prefix-tree ├── README.md ├── main.java ├── main.cpp └── main_1.cpp ├── 331. verify-preorder-serialization-of-a-binary-tree ├── README.md └── main.cpp ├── 38. count-and-say ├── README.md ├── main.cpp └── main_1.cpp ├── 1. two-sum ├── README.md └── main.cpp ├── README.md ├── 706. design-hashmap ├── README.md └── main.cpp ├── 705. design-hashset ├── README.md ├── main.cpp ├── main_1.cpp └── main.js ├── 11. container-with-most-water ├── main.cpp └── README.md ├── 12. integer-to-roman ├── README.md └── main.cpp ├── 82. remove-duplicates-from-sorted-list-ii ├── main.py ├── main.cpp └── README.md ├── 19. remove-nth-node-from-end-of-list ├── README.md ├── main_1.cpp └── main.cpp ├── 5. longest-palindromic-substring ├── README.md └── main.cpp ├── 456. 132-pattern ├── main.cpp └── README.md ├── 3. longest-substring-without-repeating-characters ├── main.cpp └── README.md ├── 16. 3sum-closest ├── README.md └── main.cpp ├── 15. 3sum ├── README.md └── main.cpp ├── 54. spiral-matrix ├── main.cpp └── README.md ├── 304. range-sum-query-2d-immutable ├── main.cpp └── README.md ├── 2. add-two-numbers ├── main.cpp └── README.md └── 1143. longest-common-subsequence └── README.md /191. number-of-1-bits/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n: int) -> int: 3 | return bin(n).count("1") -------------------------------------------------------------------------------- /258. add-digits/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | return --num % 9 + 1; 5 | } 6 | }; -------------------------------------------------------------------------------- /50. powx-n/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | return pow(x, n); 5 | } 6 | }; -------------------------------------------------------------------------------- /224. basic-calculator/main.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {number} 4 | */ 5 | var calculate = function(s) { 6 | return eval(s); 7 | }; -------------------------------------------------------------------------------- /258. add-digits/README.md: -------------------------------------------------------------------------------- 1 | # 258. add-digits 2 | 3 | [原题链接](https://leetcode-cn.com/problems/add-digits/) 4 | 5 | 找规律,数学推导,真 NB 6 | 7 | > 2021 - 04 - 09 8 | -------------------------------------------------------------------------------- /10. regular-expression-matching/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | return regex_match(s, regex(p)); 5 | } 6 | }; -------------------------------------------------------------------------------- /28. implement-strstr/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string haystack, string needle) { 4 | return haystack.find(needle); 5 | } 6 | }; -------------------------------------------------------------------------------- /1512. number-of-good-pairs/README.md: -------------------------------------------------------------------------------- 1 | # 1512. number-of-good-pairs 2 | 3 | [原题链接](https://leetcode-cn.com/problems/number-of-good-pairs/) 4 | 5 | 尝龟,暴力 6 | 7 | > 2021 - 04 - 07 8 | -------------------------------------------------------------------------------- /191. number-of-1-bits/main.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int HammingWeight(uint n) { 3 | return n == 0 ? 0 : HammingWeight((n & (~n + 1)) ^ n) + 1; 4 | } 5 | } -------------------------------------------------------------------------------- /263. ugly-number/README.md: -------------------------------------------------------------------------------- 1 | # 263. ugly-number 2 | 3 | [原题链接](https://leetcode-cn.com/problems/ugly-number/) 4 | 5 | 丑数,从 II 题跳转到 I 题,先做完这个再说(主要是简单) 6 | 7 | > 2021 - 04 - 11 8 | -------------------------------------------------------------------------------- /206. reverse-linked-list/README.md: -------------------------------------------------------------------------------- 1 | # 206. reverse-linked-list 2 | 3 | [原题链接](https://leetcode-cn.com/problems/reverse-linked-list/) 4 | 5 | 遇到链表便上 vector 开始不讲武德 6 | 7 | > 2021 - 03 - 29 8 | -------------------------------------------------------------------------------- /141. linked-list-cycle/README.md: -------------------------------------------------------------------------------- 1 | # 141. linked-list-cycle 2 | 3 | [原题链接](https://leetcode-cn.com/problems/linked-list-cycle/) 4 | 5 | 这题算是通过题干中的限制条件投机取巧了吧,写两种投机取巧的思路 6 | 7 | > 2021 - 04 - 03 8 | -------------------------------------------------------------------------------- /234. palindrome-linked-list/README.md: -------------------------------------------------------------------------------- 1 | # 234. palindrome-linked-list 2 | 3 | [原题链接](https://leetcode-cn.com/problems/palindrome-linked-list/) 4 | 5 | 继续上 vector 开始不讲武德 6 | 7 | > 2021 - 03 - 29 8 | -------------------------------------------------------------------------------- /58. length-of-last-word/README.md: -------------------------------------------------------------------------------- 1 | # 58. length-of-last-word 2 | 3 | [原题链接](https://leetcode-cn.com/problems/length-of-last-word/) 4 | 5 | 复健第一天,stringstream YYDS ! 6 | 7 | > 2021 - 07 - 07 8 | -------------------------------------------------------------------------------- /59. spiral-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 | # 59. spiral-matrix-ii 2 | 3 | [原题链接](https://leetcode-cn.com/problems/spiral-matrix-ii/) 4 | 5 | 有了昨天题目的经验,稍加改动就能做成了 6 | 不过就是效率有点低……不管了~ 7 | 8 | > 2021 - 03 - 16 9 | -------------------------------------------------------------------------------- /190. reverse-bits/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseBits(self, n: int) -> int: 3 | bits = list('{0:b}'.format(n).zfill(32)) 4 | bits.reverse() 5 | return int(''.join(bits), base=2) -------------------------------------------------------------------------------- /191. number-of-1-bits/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int iNum = 0; 5 | while (n) n &= n - 1, iNum++; 6 | return iNum; 7 | } 8 | }; -------------------------------------------------------------------------------- /28. implement-strstr/README.md: -------------------------------------------------------------------------------- 1 | # 28. implement-strstr 2 | 3 | [原题链接](https://leetcode-cn.com/problems/implement-strstr/) 4 | 5 | 今天摸鱼太多了,做一道简单题目就先去看书啦 6 | 这道题先不讲武德一次,稍后再考虑更标准的解法 7 | 8 | > 2021 - 03 - 20 9 | -------------------------------------------------------------------------------- /LCP 01. guess-numbers/README.md: -------------------------------------------------------------------------------- 1 | # LCP 01. guess-numbers 2 | 3 | [原题链接](https://leetcode-cn.com/problems/guess-numbers/) 4 | 5 | 力扣有史以来最简单的题目 6 | 7 | 不得不说布尔转 int 是用的真巧妙 8 | 9 | > 2021 - 04 - 08 10 | -------------------------------------------------------------------------------- /剑指 Offer 24. fan-zhuan-lian-biao-lcof/README.md: -------------------------------------------------------------------------------- 1 | # 剑指 Offer 24. fan-zhuan-lian-biao-lcof 2 | 3 | [原题链接](https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/) 4 | 5 | 一言难尽,明日加油! 6 | 7 | > 2021 - 06 - 14 8 | -------------------------------------------------------------------------------- /283. move-zeroes/README.md: -------------------------------------------------------------------------------- 1 | # 283. move-zeroes 2 | 3 | [原题链接](https://leetcode-cn.com/problems/move-zeroes/) 4 | 5 | 没有用双指针啥的,直接先 `erase-remove` 大法删除全部 0 6 | 然后根据 vector 原长度把 0 补全,伪移动 7 | 8 | > 2021 - 04 - 04 9 | -------------------------------------------------------------------------------- /剑指 Offer 58 - II. zuo-xuan-zhuan-zi-fu-chuan-lcof/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseLeftWords(string s, int n) { 4 | return s.substr(n, s.size() - n) + s.substr(0, n); 5 | } 6 | }; -------------------------------------------------------------------------------- /136. single-number/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int iResult = 0; 5 | for (int num : nums) iResult ^= num; 6 | return iResult; 7 | } 8 | }; -------------------------------------------------------------------------------- /1822. sign-of-the-product-of-an-array/README.md: -------------------------------------------------------------------------------- 1 | # 1822. sign-of-the-product-of-an-array 2 | 3 | [原题链接](https://leetcode-cn.com/problems/sign-of-the-product-of-an-array/) 4 | 5 | 开发繁忙,刷题懈怠了,罪过罪过…… 6 | 7 | > 2021 - 04 - 19 8 | -------------------------------------------------------------------------------- /88. merge-sorted-array/README.md: -------------------------------------------------------------------------------- 1 | # 88. merge-sorted-array 2 | 3 | [原题链接](https://leetcode-cn.com/problems/merge-sorted-array/) 4 | 5 | 还是手痒所以又摸了一道简单难度的题目 6 | 7 | 终于碰到真正 `简单` 难度的简单题目了,蕾姆 8 | 9 | > 2021 - 03 - 27 10 | -------------------------------------------------------------------------------- /LCP 01. guess-numbers/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int game(vector& guess, vector& answer) { 4 | return (guess[0] == answer[0]) + (guess[1] == answer[1]) + (guess[2] == answer[2]); 5 | } 6 | }; -------------------------------------------------------------------------------- /1006. clumsy-factorial/README.md: -------------------------------------------------------------------------------- 1 | # 1006. clumsy-factorial 2 | 3 | [原题链接](https://leetcode-cn.com/problems/clumsy-factorial/) 4 | 5 | 我有理由怀疑今天愚人节力扣把简单难度的题目标注成了中等难度 6 | 7 | (虽然在分组时搞错了加减号导致排错排了半天) 8 | 9 | > 2021 - 04 - 01 10 | -------------------------------------------------------------------------------- /173. binary-search-tree-iterator/README.md: -------------------------------------------------------------------------------- 1 | # 173. binary-search-tree-iterator 2 | 3 | [原题链接](https://leetcode-cn.com/problems/binary-search-tree-iterator/) 4 | 5 | (看了题解之后)感觉算不上 `中等` 难度的题目,至少比他的兄弟姐妹们拉胯多了 6 | 7 | > 2021 - 03 - 28 8 | -------------------------------------------------------------------------------- /191. number-of-1-bits/main.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | return n == 0 ? 0 : hammingWeight((n & (~n + 1)) ^ n) + 1; 5 | } 6 | } -------------------------------------------------------------------------------- /27. remove-element/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | nums.erase(remove(nums.begin(), nums.end(), val), nums.end()); 5 | return nums.size(); 6 | } 7 | }; -------------------------------------------------------------------------------- /29. divide-two-integers/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | if (dividend == INT_MIN && divisor == -1) return INT_MAX; 5 | return dividend / divisor; 6 | } 7 | }; -------------------------------------------------------------------------------- /70. climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 | # 70. climbing-stairs 2 | 3 | [原题链接](https://leetcode-cn.com/problems/climbing-stairs/) 4 | 5 | 罪过罪过,愿想今天不刷了结果又开始刷题了 6 | 7 | 核心思想:递推公式 `f(x) = f(x-1) + f(x-2)` 8 | 9 | > 2021 - 03 - 30 10 | -------------------------------------------------------------------------------- /8. string-to-integer-atoi/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string s) { 4 | long _ltmp = atol(s.c_str()); 5 | return _ltmp < INT_MIN ? INT_MIN : (_ltmp > INT_MAX ? INT_MAX : _ltmp); 6 | } 7 | }; -------------------------------------------------------------------------------- /169. majority-element/README.md: -------------------------------------------------------------------------------- 1 | # 169. majority-element 2 | 3 | [原题链接](https://leetcode-cn.com/problems/majority-element/) 4 | 5 | 虽然不懂 `if (iLength == 1 || iLength == 2)` 比 `if (iLength < 3)` 快了那么多,但是题做出来了就好 6 | 7 | > 2021 - 03 - 29 8 | -------------------------------------------------------------------------------- /剑指 Offer 58 - II. zuo-xuan-zhuan-zi-fu-chuan-lcof/README.md: -------------------------------------------------------------------------------- 1 | # 剑指 Offer 58 - II. zuo-xuan-zhuan-zi-fu-chuan-lcof 2 | 3 | [原题链接](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/) 4 | 5 | 右手就行,尝龟 6 | 7 | > 2021 - 04 - 20 8 | -------------------------------------------------------------------------------- /70. climbing-stairs/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | int p = 0, q = 0, r = 1; 5 | for (int i = 0; i < n; i++) 6 | p = q, q = r, r = p + q; 7 | return r; 8 | } 9 | }; -------------------------------------------------------------------------------- /1480. running-sum-of-1d-array/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector& nums) { 4 | for (int i = 1; i < nums.size(); i++) 5 | nums[i] += nums[i - 1]; 6 | return nums; 7 | } 8 | }; -------------------------------------------------------------------------------- /21. merge-two-sorted-lists/README.md: -------------------------------------------------------------------------------- 1 | # 21. merge-two-sorted-lists 2 | 3 | [原题链接](https://leetcode-cn.com/problems/merge-two-sorted-lists/) 4 | 5 | 简单难度的题目确实够简单 6 | 虽然可能不是最佳思路 7 | 但是从提交结果上看足够用啦 8 | 就这样吧~ 9 | 10 | > 2020 - 03 - 10 11 | -------------------------------------------------------------------------------- /303. range-sum-query-immutable/README.md: -------------------------------------------------------------------------------- 1 | # 303. range-sum-query-immutable 2 | 3 | [原题链接](https://leetcode-cn.com/problems/range-sum-query-immutable/) 4 | 5 | 这题……示例说的这么悬,其实 `有🖐⑨✨` 6 | 7 | 怎么说呢,魔幻开局,第一天这么顺利,加油! 8 | 9 | > 2021 - 03 - 01 10 | -------------------------------------------------------------------------------- /80. remove-duplicates-from-sorted-array-ii/README.md: -------------------------------------------------------------------------------- 1 | # 80. remove-duplicates-from-sorted-array-ii 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/) 4 | 5 | 好家伙,利用 for-each 特性是真滴牛 6 | 7 | > 2021 - 04 - 06 8 | -------------------------------------------------------------------------------- /18. 4sum/README.md: -------------------------------------------------------------------------------- 1 | # 18. 4sum 2 | 3 | [原题链接](https://leetcode-cn.com/problems/4sum/) 4 | 5 | 下意识四重循环暴力解题,在想到首先排序或许可以简化问题但是不知道如何下笔之后求助题解…… 6 | 7 | Emmmm,还是官方题解香 8 | 9 | 至于那些喷官方题解没有新意的妖魔鬼怪大神们……菜鸡这就告退 10 | 11 | *告辞!* 12 | 13 | > 2021 - 03 - 03 14 | -------------------------------------------------------------------------------- /1822. sign-of-the-product-of-an-array/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | stringstream sstream(s); 5 | string temp; 6 | while (sstream >> temp); 7 | return temp.size(); 8 | } 9 | }; -------------------------------------------------------------------------------- /4. median-of-two-sorted-arrays/README.md: -------------------------------------------------------------------------------- 1 | # 4. median-of-two-sorted-arrays 2 | 3 | [原题链接](https://leetcode-cn.com/problems/median-of-two-sorted-arrays/) 4 | 5 | 为什么难度标注为 `困难` ? 6 | 我寻思这题用 STL 不是有手就行? 7 | (虽然被因为忘了 C++ 从 0 开始的索引搞崩了心态) 8 | 9 | > 2021 - 03 - 04 -------------------------------------------------------------------------------- /74. search-a-2d-matrix/README.md: -------------------------------------------------------------------------------- 1 | # 74. search-a-2d-matrix 2 | 3 | [原题链接](https://leetcode-cn.com/problems/search-a-2d-matrix/) 4 | 5 | 已排序查找,下意识想到了二分法,但是还是不能拒绝暴力的诱惑 6 | 7 | 先整一版暴力代码,然后搞二分法 8 | 9 | 另:用 STL 写二分法是真舒服 10 | 11 | > 2021 - 03 - 30 12 | -------------------------------------------------------------------------------- /1431. kids-with-the-greatest-number-of-candies/README.md: -------------------------------------------------------------------------------- 1 | # 1431. kids-with-the-greatest-number-of-candies 2 | 3 | [原题链接](https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/) 4 | 5 | 搜了一下果然有,学到了 `max_element` 的用法 6 | 7 | > 2021 - 04 - 12 8 | -------------------------------------------------------------------------------- /190. reverse-bits/README.md: -------------------------------------------------------------------------------- 1 | # 190. reverse-bits 2 | 3 | [原题链接](https://leetcode-cn.com/problems/reverse-bits/) 4 | 5 | ***二进制运算永远滴痛*** 6 | 7 | 勉强拿 Python 做了一版简单的,稍后研究 C++ 该怎么写 8 | 9 | 看题解思维惯性了用掩码等一片位运算,其实用 bitset 实现还算容易 10 | 11 | > 2021 - 03 - 29 12 | -------------------------------------------------------------------------------- /面试题 17.21. volume-of-histogram-lcci/README.md: -------------------------------------------------------------------------------- 1 | # 面试题 17.21. volume-of-histogram-lcci 2 | 3 | [原题链接](https://leetcode-cn.com/problems/volume-of-histogram-lcci/) 4 | 5 | 乍看感觉配不上 `困难` 难度,做了一下却感觉并没有很好的思路 6 | 7 | 日常在看完大佬们思路后惊叹! 8 | 9 | > 2021 - 04 - 02 10 | -------------------------------------------------------------------------------- /1480. running-sum-of-1d-array/README.md: -------------------------------------------------------------------------------- 1 | # 1480. running-sum-of-1d-array 2 | 3 | [原题链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/) 4 | 5 | 我以为我的代码会是最简单最高效的,看了评论区之后才发现不是 6 | 7 | 学到了,返回 vector 的时候可以尝试直接用传入的 vector 8 | 9 | > 2021 - 04 - 16 10 | -------------------------------------------------------------------------------- /1672. richest-customer-wealth/README.md: -------------------------------------------------------------------------------- 1 | # 1672. richest-customer-wealth 2 | 3 | [原题链接](https://leetcode-cn.com/problems/richest-customer-wealth/) 4 | 5 | 笑死,为了极限压行选了复杂度极大的写法 6 | 7 | 但是为啥运行时间还是打败了 94.54% 的用户,内存消耗打败了 71.78% 的用户 8 | 9 | > 2021 - 04 - 10 10 | -------------------------------------------------------------------------------- /155. min-stack/README.md: -------------------------------------------------------------------------------- 1 | # 155. min-stack 2 | 3 | [原题链接](https://leetcode-cn.com/problems/min-stack/) 4 | 5 | 今天刷题上头了,再加上天气热,热的我做他事心思全无,不如提前把明天的题也刷了吧 6 | 7 | 最小栈,要求常数时间找到最小值,写了一版没有常数时间的代码,稍后完成辅助栈的常数时间查找 8 | 9 | 另:Python 版本实现起来是真滴简单 10 | 11 | > 2021 - 03 - 29 12 | -------------------------------------------------------------------------------- /224. basic-calculator/README.md: -------------------------------------------------------------------------------- 1 | # 224. basic-calculator 2 | 3 | [原题链接](https://leetcode-cn.com/problems/basic-calculator/) 4 | 5 | 今天的每日一题终于又回到了 `困难` 难度 6 | 果然是我日常不熟悉的表达式求值 7 | 看题解翻评论区发现一个不讲武德的 JS 版本 8 | 我直接先写为敬 9 | 后续再实现 C++ 版本 10 | 11 | > 2020 - 03 - 10 12 | -------------------------------------------------------------------------------- /面试题 02.03. delete-middle-node-lcci/README.md: -------------------------------------------------------------------------------- 1 | # 面试题 02.03. delete-middle-node-lcci 2 | 3 | [原题链接](https://leetcode-cn.com/problems/delete-middle-node-lcci/) 4 | 5 | 建议修改题目难度为 `困难`,因为题意太难理解了 6 | 7 | 还《程序员面试金典(第 6 版)》独家授权,这种辣鸡描述的题目是给人做的吗? 8 | 9 | > 2021 - 04 - 21 10 | -------------------------------------------------------------------------------- /263. ugly-number/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int n) { 4 | if (n < 1) return false; 5 | while (!(n % 5)) n /= 5; 6 | while (!(n % 3)) n /= 3; 7 | while (!(n % 2)) n /= 2; 8 | return n == 1; 9 | } 10 | }; -------------------------------------------------------------------------------- /50. powx-n/main_2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | double dResult = 1.0; 5 | for (int i = n; i; i /= 2) 6 | (i & 1) ? (dResult *= x, x *= x) : x*= x; 7 | return n < 0 ? 1 / dResult : dResult; 8 | } 9 | }; -------------------------------------------------------------------------------- /88. merge-sorted-array/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | nums1.resize(m); 5 | for (int i = 0; i < n; i++) nums1.push_back(nums2[i]); 6 | sort(nums1.begin(), nums1.end()); 7 | } 8 | }; -------------------------------------------------------------------------------- /35. search-insert-position/README.md: -------------------------------------------------------------------------------- 1 | # 35. search-insert-position 2 | 3 | [原题链接](https://leetcode-cn.com/problems/search-insert-position/) 4 | 5 | 新书:《简单难度》 6 | 力扣就是这样,就算是简单难度也有坑 7 | 无语,题解的思路是用了二分法 8 | 用吧,至少比自己的暴力要优美很多 ~ 9 | 10 | 今天也是极限压行的一天呢! 11 | 12 | > 2021 - 03 - 24 13 | -------------------------------------------------------------------------------- /17. letter-combinations-of-a-phone-number/README.md: -------------------------------------------------------------------------------- 1 | # 17. letter-combinations-of-a-phone-number 2 | 3 | [原题链接](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/) 4 | 5 | 今天又是极限压行的一天呢! 6 | 感觉现在已经对 0ms 和 100% 麻木了 7 | 虽然这次是在答案的基础上改进过来的…… 8 | 9 | > 2020 - 03 - 10 10 | -------------------------------------------------------------------------------- /27. remove-element/README.md: -------------------------------------------------------------------------------- 1 | # 27. remove-element 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-element/) 4 | 5 | 比起 26. 移除相同元素,这道题里需要移除的元素是单一且固定的 6 | 所以直接使用 STL 的 `remove` 配合 `erase` 就可以轻易完成了 7 | 不过总感觉有点偏离出题者意图……稍后看看常规解法 8 | 9 | 常规思路:依然是双指针 10 | 11 | > 2021 - 03 - 11 12 | -------------------------------------------------------------------------------- /29. divide-two-integers/README.md: -------------------------------------------------------------------------------- 1 | # 29. divide-two-integers 2 | 3 | [原题链接](https://leetcode-cn.com/problems/divide-two-integers/) 4 | 5 | 刚做了一道难度还可以的中等难度题目,马上给我来了道困难难度的中等题 6 | 看了一圈题解,虽然不愧是大佬们各显神通,但是终究都没有严格遵循题目要求的32位有符号整数环境 7 | 既然如此,那我没用 long 但是用了除法也不过分吧(笑死 8 | 9 | > 2021 - 03 - 28 10 | -------------------------------------------------------------------------------- /283. move-zeroes/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int iLength = nums.size(); 5 | nums.erase(remove(nums.begin(), nums.end(), 0), nums.end()); 6 | while (nums.size() != iLength) 7 | nums.push_back(0); 8 | } 9 | }; -------------------------------------------------------------------------------- /6. zigzag-conversion/README.md: -------------------------------------------------------------------------------- 1 | # 6. zigzag-conversion 2 | 3 | [原题链接](https://leetcode-cn.com/problems/zigzag-conversion/) 4 | 5 | 看似拙劣但是效率还可以的算法,毕竟执行时间打败了 89.70% 的队友 6 | (虽然内存消耗只打败了 34.36% ) 7 | 8 | 另:当行数等于 2 时的特殊情况没有说明白是属实烂题,好歹放到示例里面也行啊,非得自己跑一下出错了看看你的预期输出才知道?!憨批题! 9 | 10 | > 2021 - 03 - 05 -------------------------------------------------------------------------------- /1603. design-parking-system/README.md: -------------------------------------------------------------------------------- 1 | # 1603. design-parking-system 2 | 3 | [原题链接](https://leetcode-cn.com/problems/design-parking-system/) 4 | 5 | 罪过罪过,这几天过于散漫了以至于咕了好多天没有认真刷题(虽然如此,但是开发依旧没有落下) 6 | 愿想明天再开始认真刷题,就在要关电脑的时候看了一眼原来今天的每日一题这么简单 7 | 所幸刷完再睡,不!还得去洗澡,淦啊!困死啦…… 8 | 9 | > 2021 - 03 - 19 10 | -------------------------------------------------------------------------------- /80. remove-duplicates-from-sorted-array-ii/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int i = 0; 5 | for (int& n : nums) 6 | if (i < 2 || n > nums[i - 2]) 7 | nums[i++] = n; 8 | return i; 9 | } 10 | }; -------------------------------------------------------------------------------- /227. basic-calculator-ii/README.md: -------------------------------------------------------------------------------- 1 | # 227. basic-calculator-ii 2 | 3 | [原题链接](https://leetcode-cn.com/problems/basic-calculator-ii/) 4 | 5 | 刚想说有谁能拒绝一个简单的 `eval()` 呢 6 | 却没想到测试用例的要求是对每一步运算的结果都取整 7 | 所以昨天爽了一把的 JS 大法今天爆炸了 8 | 只能稳稳妥妥地学习 C++ 的栈实现 9 | 10 | 另:`accumulate` 的用法是新知识 11 | 12 | > 2020 - 03 - 11 13 | -------------------------------------------------------------------------------- /341. flatten-nested-list-iterator/README.md: -------------------------------------------------------------------------------- 1 | # 341. flatten-nested-list-iterator 2 | 3 | [原题链接](https://leetcode-cn.com/problems/flatten-nested-list-iterator/) 4 | 5 | 一瞬间找回了前几天写 Lua 的 JSON 库时递归解析的感觉 6 | 虽然提前遍历了一遍导致空间复杂度上去了 7 | 但是由于力扣的 OJ 不把初始化的时间计入时间消耗 8 | 所以这个解法也拿到了打败 70.37% 的时间成绩 9 | 10 | > 2021 - 03 - 23 11 | -------------------------------------------------------------------------------- /61. rotate-list/README.md: -------------------------------------------------------------------------------- 1 | # 61. rotate-list 2 | 3 | [原题链接](https://leetcode-cn.com/problems/rotate-list/) 4 | 5 | 晚上做题脑子糊涂,在锐澳和茶以及昨夜四个多小时的睡眠多重作用下, 6 | 极其简单的语句也被我写得七扭八歪起来了,还好趁着意识清醒的瞬间改了过来 7 | 8 | 虽然算法上偷懒了(遍历链表将节点指针存储到 vector)中, 9 | 但是至少是自己做出来的 `中等` 难度的题目(可喜可贺~) 10 | 11 | 今日任务完结撒花! 12 | 13 | > 2021 - 03 - 27 14 | -------------------------------------------------------------------------------- /83. remove-duplicates-from-sorted-list/README.md: -------------------------------------------------------------------------------- 1 | # 83. remove-duplicates-from-sorted-list 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/) 4 | 5 | 好家伙,这波直接反向操作先难后简,`我逐渐理解一切.jpg` 6 | 这就是把昨天写错的那份代码稍加修改便是今天的题解了 7 | 一切还算顺利(虽然最后还是查看了题解发现只需要修改当前代码的一个小地方) 8 | 9 | > 2021 - 03 - 26 10 | -------------------------------------------------------------------------------- /1047. remove-all-adjacent-duplicates-in-string/README.md: -------------------------------------------------------------------------------- 1 | # 1047. remove-all-adjacent-duplicates-in-string 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) 4 | 5 | 第一反应是拿 `'+'` 等特殊字符先做一遍字符替换 6 | 然后再使用 `erase` 等函数做擦除 7 | 看了一眼题解人直接傻掉 8 | 原来这么简单?! 9 | 10 | > 2021 - 03 - 09 11 | -------------------------------------------------------------------------------- /1047. remove-all-adjacent-duplicates-in-string/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string S) { 4 | string strResult; 5 | for (char c : S) 6 | !strResult.empty() && strResult.back() == c ? strResult.pop_back() : strResult.push_back(c); 7 | return strResult; 8 | } 9 | }; -------------------------------------------------------------------------------- /190. reverse-bits/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | bitset<32> bits = bitset<32>(n); 5 | char tmp = 0; 6 | for(int i = 0; i < 16; i++) 7 | tmp = bits[i], bits[i] = bits[31-i], bits[31-i] = tmp; 8 | return bits.to_ulong(); 9 | } 10 | }; -------------------------------------------------------------------------------- /20. valid-parentheses/README.md: -------------------------------------------------------------------------------- 1 | # 20. valid-parentheses 2 | 3 | [原题链接](https://leetcode-cn.com/problems/valid-parentheses/) 4 | 5 | 虽然这道题是很久之前做的,但是由于执行用时 `0ms` 并且击败了 `100.00%` 的用户 6 | 而内存消耗 `6.3MB` 并且击败了 `53.78%` 的用户 7 | 算得上是个人刷题史上到目前为止很少出现的奇迹了 8 | 存在这里记录一下,希望自己接下来可以一如既往地奇迹下去吧 9 | 10 | 加油! 11 | 12 | > 2020 - 09 - 21 13 | -------------------------------------------------------------------------------- /338. counting-bits/README.md: -------------------------------------------------------------------------------- 1 | # 338. counting-bits 2 | 3 | [原题链接](https://leetcode-cn.com/problems/counting-bits/) 4 | 5 | 下意识想用 “优美的” 解题方法 6 | 于是搜了一下 C++ 的十进制转二进制算法 7 | 学到了模板 bitset 的用法 8 | ~~不得不说 bitset 不能使用精简 for 循环是真滴坑~~ 9 | 10 | 另:中等难度的题目 `有手就行` 的错觉又回来了 11 | 12 | 稍后写一个不使用模板类或内置函数的二进制转换版本 13 | 14 | > 2021 - 03 - 03 15 | -------------------------------------------------------------------------------- /53. maximum-subarray/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int iResult = nums[0], iSum = 0; 5 | for (int num : nums) { 6 | iSum > 0 ? iSum += num : iSum = num; 7 | iResult = max(iResult, iSum); 8 | } 9 | return iResult; 10 | } 11 | }; -------------------------------------------------------------------------------- /23. merge-k-sorted-lists/README.md: -------------------------------------------------------------------------------- 1 | # 23. merge-k-sorted-lists 2 | 3 | [原题链接](https://leetcode-cn.com/problems/merge-k-sorted-lists/) 4 | 5 | 这是一道被我放过去好多天的 `困难` 难度的题目 6 | 7 | 但是暴力解法思路简直不要太简单: 8 | 9 | + 首先遍历每个链表将节点指针保存到 vector 中 10 | + 然后对 vector 中的节点指针进行排序 11 | + 遍历 vector 生成新链表 12 | 13 | **注意处理 vector 为空和链表头结点为空的情况** 14 | 15 | > 2021 - 03 - 28 16 | -------------------------------------------------------------------------------- /101. symmetric-tree/README.md: -------------------------------------------------------------------------------- 1 | # 101. symmetric-tree 2 | 3 | [原题链接](https://leetcode-cn.com/problems/symmetric-tree/) 4 | 5 | 笑死,极限压行: 6 | 7 | ```c++ 8 | return !pLeft && !pRight ? true : (!pLeft || !pRight ? false : pLeft->val == pRight->val && check(pLeft->left, pRight->right) && check(pLeft->right, pRight->left)); 9 | ``` 10 | 11 | > 2021 - 03 - 29 12 | -------------------------------------------------------------------------------- /191. number-of-1-bits/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | n = (n & 0x55555555) + ((n >> 1) & 0x55555555); 5 | n = (n & 0x33333333) + ((n >> 2) & 0x33333333); 6 | n = (n & 0x0F0F0F0F) + ((n >> 4) & 0x0F0F0F0F); 7 | n = (n * (0x01010101) >> 24); 8 | return n; 9 | } 10 | }; -------------------------------------------------------------------------------- /26. remove-duplicates-from-sorted-array/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | int iIndexSlow = 0; 5 | for (int iIndexFast = 0; iIndexFast < nums.size(); iIndexFast++) 6 | if (nums[iIndexFast] != val) nums[iIndexSlow] = nums[iIndexFast], iIndexSlow++; 7 | return iIndexSlow; 8 | } 9 | }; -------------------------------------------------------------------------------- /1480. running-sum-of-1d-array/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector& nums) { 4 | vector vResult; 5 | int iLength = nums.size(); 6 | vResult.push_back(nums[0]); 7 | for(int i = 1; i < iLength; i++) 8 | vResult.push_back(vResult[i - 1] + nums[i]); 9 | return vResult; 10 | } 11 | }; -------------------------------------------------------------------------------- /503. next-greater-element-ii/README.md: -------------------------------------------------------------------------------- 1 | # 503. next-greater-element-ii 2 | 3 | [原题链接](https://leetcode-cn.com/problems/next-greater-element-ii/) 4 | 5 | 噗! 6 | 一看这题有手就行,但是常规思维下来时间消耗惊人,只打败了 5.04% 的用户 7 | 稍后看下其他大佬们的题解再做一版代码吧 8 | 9 | 官方题解啃完了,怎么说呢…… 10 | Emmm,总之不想用他的做法写就是了 11 | 虽然复杂度上确实有改善,但是提升并不大 12 | 而且死路也不太容易理解(至少在我看来) 13 | 本题,完结,撒花~ 14 | 15 | > 2021 - 03 - 06 16 | -------------------------------------------------------------------------------- /1512. number-of-good-pairs/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | int iLength = nums.size(), iResult = 0; 5 | for (int i = 0; i < iLength; i++) 6 | for (int j = i + 1; j < iLength; j++) 7 | if (nums[i] == nums[j]) 8 | iResult++; 9 | return iResult; 10 | } 11 | }; -------------------------------------------------------------------------------- /232. implement-queue-using-stacks/README.md: -------------------------------------------------------------------------------- 1 | # 232. implement-queue-using-stacks 2 | 3 | [原题链接](https://leetcode-cn.com/problems/implement-queue-using-stacks/) 4 | 5 | 好耶!又是一次执行用时打败 100% 的提交! 6 | 并且这次的内存消耗也打败了 79.45% ! 7 | 虽然我不讲武德了,没有使用双栈实现队列 8 | 毕竟一看到这玩意就想到 vector ,兴奋极了 9 | 10 | 用 vector 实现栈,用 vector 实现队列…… 11 | 以及用 stack 实现栈,用 queue 实现队列,以及用 vector 实现数组(x 12 | 13 | > 2021 - 03 - 05 -------------------------------------------------------------------------------- /74. search-a-2d-matrix/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | auto row = upper_bound(matrix.begin(), matrix.end(), target, [](const int b, const vector& a) {return b < a[0];}); 5 | if (row == matrix.begin()) return false; 6 | row--; return binary_search(row->begin(), row->end(), target); 7 | } 8 | }; -------------------------------------------------------------------------------- /26. remove-duplicates-from-sorted-array/README.md: -------------------------------------------------------------------------------- 1 | # 26. remove-duplicates-from-sorted-array 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/) 4 | 5 | 跳过了接下来的几道中等和困难难度的题目,打算搞一道简单的题目来放松一下 6 | 结果…… TMD 这是个简单难度的题目?! 7 | 翻了一下评论区果然不止我一个人有这种感觉 8 | 仔细读了一下判题要求才知道并不需要真正地从 vector 中移除元素 9 | 否则耗时可能就不止这么一点了 10 | 11 | 双指针方法:慢指针和快指针是真滴好用,吹爆! 12 | 13 | > 2020 - 03 - 11 14 | -------------------------------------------------------------------------------- /50. powx-n/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double quickMul(double x, long long N) { 4 | if (!N) return 1; 5 | double dTemp = quickMul(x, N / 2); 6 | return N % 2 ? dTemp * dTemp * x : dTemp * dTemp; 7 | } 8 | double myPow(double x, int n) { 9 | long long N = n; 10 | return N >= 0 ? quickMul(x, N) : 1 / quickMul(x, -N); 11 | } 12 | }; -------------------------------------------------------------------------------- /1431. kids-with-the-greatest-number-of-candies/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kidsWithCandies(vector& candies, int extraCandies) { 4 | vector vResult; 5 | int iMax = *max_element(candies.begin(), candies.end()); 6 | for (int candy : candies) 7 | vResult.push_back(candy + extraCandies >= iMax); 8 | return vResult; 9 | } 10 | }; -------------------------------------------------------------------------------- /面试题 02.03. delete-middle-node-lcci/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | node->val = node->next->val; 13 | node->next = node->next->next; 14 | } 15 | }; -------------------------------------------------------------------------------- /1672. richest-customer-wealth/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumWealth(vector>& accounts) { 4 | sort(accounts.begin(), accounts.end(), [&](const vector& a, const vector& b) { 5 | return accumulate(a.begin(), a.end(), 0) > accumulate(b.begin(), b.end(), 0); 6 | }); 7 | return accumulate(accounts[0].begin(), accounts[0].end(), 0); 8 | } 9 | }; -------------------------------------------------------------------------------- /74. search-a-2d-matrix/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int iLengthColumn = matrix.size(), iLengthRow = matrix[0].size(); 5 | for(int i = 0; i < iLengthColumn; i++) 6 | for (int j = 0; j < iLengthRow; j++) 7 | if (target == matrix[i][j]) return true; 8 | return false; 9 | } 10 | }; -------------------------------------------------------------------------------- /208. implement-trie-prefix-tree/README.md: -------------------------------------------------------------------------------- 1 | # 208. implement-trie-prefix-tree 2 | 3 | [原题链接](https://leetcode-cn.com/problems/implement-trie-prefix-tree/) 4 | 5 | 笑死,拿 vector 实现前缀树,竟然没有超时,但是跑出来的成绩属实拉胯 6 | 7 | 去学一下真正的前缀树写法~ 8 | 9 | 好家伙,还看到一个 [Java 版的四行代码神操作](https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/java-4xing-dai-ma-jian-jian-dan-dan-by-1-qsxw/),Copy 一份~ 10 | 11 | > 2021 - 04 - 14 12 | -------------------------------------------------------------------------------- /28. implement-strstr/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string haystack, string needle) { 4 | int iLengthHaystack = haystack.size(), iLengthNeedle = needle.size(); 5 | for (int index = 0; index < iLengthHaystack - iLengthNeedle + 1; index++) 6 | if (haystack.substr(index, iLengthNeedle) == needle) 7 | return index; 8 | return -1; 9 | } 10 | }; -------------------------------------------------------------------------------- /4. median-of-two-sorted-arrays/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 4 | nums1.insert(nums1.end(), nums2.begin(), nums2.end()); 5 | sort(nums1.begin(), nums1.end()); 6 | unsigned iNumCount = nums1.size(); 7 | return iNumCount % 2 ? nums1[iNumCount / 2] : (nums1[iNumCount / 2 - 1] + nums1[iNumCount / 2]) / 2.0; 8 | } 9 | }; -------------------------------------------------------------------------------- /331. verify-preorder-serialization-of-a-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | # 331. verify-preorder-serialization-of-a-binary-tree 2 | 3 | [原题链接](https://leetcode-cn.com/problems/verify-preorder-serialization-of-a-binary-tree/) 4 | 5 | 不愧是植树节,今天的每日一题便是树的前序遍历 6 | 在顺理成章地看完题解后意识到,似乎递归和手写栈在大部分情况下可以自由转换 7 | 不多说了,继续研究官方题解了~ 8 | 9 | 另外,这道题竟然可以做到连栈都不用是真滴秀…… 10 | 11 | 由于最近在造 Lua 扩展库的轮子,所以先只刷这一道题吧~ 12 | 13 | 今天又是极限压行和 100% 的一天呢! 14 | 15 | > 2021 - 03 - 12 16 | -------------------------------------------------------------------------------- /38. count-and-say/README.md: -------------------------------------------------------------------------------- 1 | # 38. count-and-say 2 | 3 | [原题链接](https://leetcode-cn.com/problems/count-and-say/) 4 | 5 | 力扣,你管这叫 `简单` 题目?! 6 | 要不是之前做过一道类似的题目不然还真被你坑坏了…… 7 | 8 | 好家伙,又看到两个狼灭直接打印出来返回现有的数据,我也来一份 ~ 9 | + 其中之一:https://leetcode-cn.com/problems/count-and-say/solution/da-biao-ju-ran-4ms-by-ninjayahoo/ 10 | + 另外一个:https://leetcode-cn.com/problems/count-and-say/solution/bao-li-mei-ju-by-joyce-t-cin5/ 11 | 12 | > 2021 - 03 - 23 13 | -------------------------------------------------------------------------------- /1. two-sum/README.md: -------------------------------------------------------------------------------- 1 | # 1. two-sum 2 | 3 | [原题链接](https://leetcode-cn.com/problems/two-sum/) 4 | 5 | 其实一开始没打算刷这道题的,因为他很久之前就被我刷过了 6 | 只是因为 GayHub 神奇的文件名排序方式让我在提交了 10. 后想整个在他之前的数字 7 | 于是重新打开了这道题,发现虽然作为 01 号,但是并不是那种幼儿园水平的题 8 | 于是重新做了一下,提交后发现…… 9 | 10 | | 答题时间 | 执行用时 | 内存消耗 | 11 | |:--------:|:--------:|:--------:| 12 | | 六个月前 | 848 ms | 9.1 MB | 13 | | 今天 | 8 ms | 8.7 MB | 14 | 15 | 不得不说,用心刷题时提升还是很大的(x 16 | 17 | > 2021 - 03 - 07 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetcodeEveryday • 力扣通关,大厂坐穿 2 | 3 | *这个仓库已经停止维护啦,来我博客看看吧:[Voidmatrix's Blog](https://www.voidmatrix.work/)* 4 | 5 | > 菜鸡的力扣每日刷题汇总~ 6 | 7 | + 题目难度不固定,选题看心情(多半为每日推荐和顺序刷题) 8 | 9 | + 解法可能参考写法优美的答案,也可能不堪入目 10 | 11 | + 尽可能使用 C++ 编写,~~如果足够简单~~也可能使用其他语言秀一下 12 | 13 | + 每题文件夹内 `README.md` 中为过程性代码和吐槽等,单独源码文件内的代码为正解 14 | 15 | + ~~每日必刷一题 `(Flag 在此!)`~~ 16 | 17 | + ~~因为太菜了,所以决定每日必刷两题`(Flag 在此!)`~~ 18 | 19 | + 题也太多了吧……每日必刷三道题!`(Flag 在此!)` 20 | -------------------------------------------------------------------------------- /338. counting-bits/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int num) { 4 | vector vResults; 5 | for (int i = 0; i <= num; i++) { 6 | int _iNum = i, _iCount = 0; 7 | while (_iNum) { 8 | if (_iNum % 2) _iCount++; 9 | _iNum /= 2; 10 | } 11 | vResults.push_back(_iCount); 12 | } 13 | return vResults; 14 | } 15 | }; -------------------------------------------------------------------------------- /136. single-number/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int iLength = nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | if (iLength == 1 || nums[0] != nums[1]) return nums[0]; 7 | for (int i = 1; i < iLength - 1; i++) 8 | if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) 9 | return nums[i]; 10 | return nums[iLength - 1]; 11 | } 12 | }; -------------------------------------------------------------------------------- /706. design-hashmap/README.md: -------------------------------------------------------------------------------- 1 | # 706. design-hashmap 2 | 3 | [原题链接](https://leetcode-cn.com/problems/design-hashmap/) 4 | 5 | 可能是上天可怜我被正在开发的 [LuaJsonLib](https://github.com/VoidmatrixHeathcliff/LuaLibs/tree/main/LuaJsonLib) 折磨所以给我了一道简单难度的每日一题吧 6 | 既然是简单难度,那么我就不讲武德了(虽然没有用内置模板因为题目明令禁止了QwQ) 7 | 自定义结构体,使用 vector 循环遍历…… 8 | 虽然性能拉胯但是真滴 `简单` 啊,好做就完事了! 9 | 继续接受 [LuaJsonLib](https://github.com/VoidmatrixHeathcliff/LuaLibs/tree/main/LuaJsonLib) 的折磨…… 10 | 11 | > 2021 - 03 - 14 12 | -------------------------------------------------------------------------------- /705. design-hashset/README.md: -------------------------------------------------------------------------------- 1 | # 705. design-hashset 2 | 3 | [原题链接](https://leetcode-cn.com/problems/design-hashset/) 4 | 5 | 日常简单题放松 6 | 乍一看没想到用内置哈希表,寻思这么简单直接暴力判断完事了 7 | 但是这题偏偏给我来个 `你可以不使用内建的哈希集合库解决此问题吗?`的进阶提示 8 | 我直接拿来 `unordered_map` 用,不用岂不是对不起这简单的难度 9 | (虽然由于对 unordered_map 的不熟悉导致并没有简单多少) 10 | 11 | 另,顺便拿暴力写一下…… 12 | 13 | 等等……为什么用 vector 实现的这么拉胯…… 14 | 不得不去看看大佬们的题解了,争取做个优秀点的版本 15 | 16 | 撅了,我寻思这是道简单难度的题目啊…… 17 | 算了,拿其他语言的内建哈希表玩玩…… 18 | 19 | > 2021 - 03 - 13 20 | -------------------------------------------------------------------------------- /141. linked-list-cycle/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | int iNum = 0; 13 | while (head && iNum <= 100000) { 14 | iNum++; 15 | head = head->next; 16 | } 17 | return iNum > 100000; 18 | } 19 | }; -------------------------------------------------------------------------------- /11. container-with-most-water/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int iMaxArea = 0, iIndexLeft = 0, iIndexRight = height.size() - 1; 5 | while (iIndexLeft < iIndexRight) { 6 | iMaxArea = max(iMaxArea, min(height[iIndexLeft], height[iIndexRight]) * (iIndexRight - iIndexLeft)); 7 | height[iIndexLeft] <= height[iIndexRight] ? iIndexLeft++ : iIndexRight--; 8 | } 9 | return iMaxArea; 10 | } 11 | }; -------------------------------------------------------------------------------- /338. counting-bits/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int num) { 4 | vector vResults; 5 | for (int i = 0; i <= num; i++) { 6 | bitset::digits> _bits(i); 7 | int _iNums = 0; 8 | for (int j = 0; j < _bits.size(); j++) { 9 | if (_bits[j]) _iNums++; 10 | } 11 | vResults.push_back(_iNums); 12 | } 13 | return vResults; 14 | } 15 | }; -------------------------------------------------------------------------------- /136. single-number/README.md: -------------------------------------------------------------------------------- 1 | # 136. single-number 2 | 3 | [原题链接](https://leetcode-cn.com/problems/single-number/) 4 | 5 | 确实,不看答案的情况下唯一能想到的就是先排序再遍历 6 | 看了答案才知道这玩意儿也能位运算就 NM 离谱 7 | 8 | 异或运算有以下三个性质: 9 | 10 | + 任何数和 0 做异或运算,结果仍然是原来的数,即 `a⊕0=a` 11 | + 任何数和其自身做异或运算,结果是 0 ,即 `a⊕a=0` 12 | + 异或运算满足交换律和结合律,即 `a⊕b⊕a=b⊕a⊕a=b⊕(a⊕a)=b⊕0=b` 13 | 14 | 其余题解见:https://leetcode-cn.com/problems/single-number/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-solution/ 15 | 16 | ***数学推导真滴神!*** 17 | 18 | > 2021 - 03 - 29 19 | -------------------------------------------------------------------------------- /26. remove-duplicates-from-sorted-array/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int iLength = nums.size(), iIndexSlow = 0; 6 | if (!iLength) return 0; 7 | for (int iIndexFast = 1; iIndexFast < iLength; iIndexFast++) 8 | if (nums[iIndexSlow] != nums[iIndexFast]) 9 | iIndexSlow++, nums[iIndexSlow] = nums[iIndexFast]; 10 | return iIndexSlow + 1; 11 | } 12 | }; -------------------------------------------------------------------------------- /35. search-insert-position/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | int iLength = nums.size(), iLeftIndex = 0, iRightIndex = iLength - 1, iResult = iLength; 5 | while (iLeftIndex <= iRightIndex) { 6 | int iMidIndex = ((iRightIndex - iLeftIndex) >> 1) + iLeftIndex; 7 | target <= nums[iMidIndex] ? iResult = iMidIndex, iRightIndex = iMidIndex - 1 : iLeftIndex = iMidIndex + 1; 8 | } 9 | return iResult; 10 | } 11 | }; -------------------------------------------------------------------------------- /12. integer-to-roman/README.md: -------------------------------------------------------------------------------- 1 | # 12. integer-to-roman 2 | 3 | [原题链接](https://leetcode-cn.com/problems/integer-to-roman/) 4 | 5 | 第一反应是拿一堆 `if...else...` 判断 6 | 看了下题解果然有大佬提供了十分优美的算法(贪心法) 7 | 优化了一下[大佬的算法](https://leetcode-cn.com/problems/integer-to-roman/solution/zheng-shu-zhuan-luo-ma-shu-zi-cshi-xian-liang-chon/),把动态数组改为静态数组 8 | 效率直接起飞~(执行用时打败 100% 用户的题目喜加一) 9 | 10 | | 提供者 | 执行用时 | 内存消耗 | 11 | |:------:|:--------:|:--------:| 12 | | 大佬 | 16ms | 8.7MB | 13 | | 我 | 0ms | 5.8MB | 14 | 15 | > 2021 - 03 - 08 16 | -------------------------------------------------------------------------------- /141. linked-list-cycle/main_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | while (head) { 13 | if (head->val == INT_MAX) 14 | return true; 15 | else 16 | head->val = INT_MAX; 17 | head = head->next; 18 | } 19 | return false; 20 | } 21 | }; -------------------------------------------------------------------------------- /8. string-to-integer-atoi/README.md: -------------------------------------------------------------------------------- 1 | # 8. string-to-integer-atoi 2 | 3 | [原题链接](https://leetcode-cn.com/problems/string-to-integer-atoi/) 4 | 5 | 我直接不讲武德 6 | 你要实现 `atoi` 并且控制 int 类型溢出的情况 7 | 那我就直接把 `atol` 拿来用了,反正你的测试用例不会溢出(XD 8 | 9 | 稍后再研究一下常规算法(但愿能学会) 10 | 11 | 看完了 [大佬的解题](https://leetcode-cn.com/problems/string-to-integer-atoi/solution/8-zi-fu-chuan-zhuan-huan-zheng-shu-c-by-o9192/) 12 | 对于极限压行有了更精辟的理解(XD 13 | 其实更重要的是对 int 溢出的预处理操作 14 | `main_1.cpp` 中是对大佬解题算法的优化(指没有多次调用 `string.size()` 以及避免了使用 `isdigit()` 函数) 15 | 16 | > 2021 - 03 - 06 17 | -------------------------------------------------------------------------------- /82. remove-duplicates-from-sorted-list-ii/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def deleteDuplicates(self, head, val=None) -> ListNode: 8 | if not head: return head 9 | if (val is not None and head.val==val) or (head.next and head.val==head.next.val): 10 | return self.deleteDuplicates(head.next, head.val) 11 | head.next = self.deleteDuplicates(head.next, head.val) 12 | return head -------------------------------------------------------------------------------- /19. remove-nth-node-from-end-of-list/README.md: -------------------------------------------------------------------------------- 1 | # 19. remove-nth-node-from-end-of-list 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/) 4 | 5 | 这道题如果奔着能做出来就行的目的去做,按道理不应该算中等难度 6 | 但是由于看到了那个挑战——只遍历一遍,所以决定这么做试试 7 | 8 | 不得不说,这个题的判题是把你返回的 head 指针作为修改后链表的头部 9 | 所以在题目要求移除链表第一个位置的节点的情况下,只需要返回第二个位置的节点指针就好了,根本不需要把第一个节点的 next 指针置 nullptr 10 | 另外,也不需要 delete 掉那个节点……看着真难受 11 | 不过为了效率还是牺牲一下吧…… 12 | 13 | 稍后试一下遍历两遍的思路…… 14 | 15 | 算了,遍历两遍好像也不是很好写…… 16 | 拿这个时间学习一下大佬们的写法吧 17 | 18 | 沃日!双指针解法直接不是人能想出来的吧!太绝了! 19 | **官方题解就是香~** 20 | 21 | > 2021 - 03 - 09 22 | -------------------------------------------------------------------------------- /12. integer-to-roman/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string intToRoman(int num) 5 | { 6 | string strResult; 7 | int iAryKeys[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 8 | const char* strAryValues[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 9 | for (int i = 0; i < 13; i++) { 10 | while (num >= iAryKeys[i]) { 11 | strResult.append(strAryValues[i]); 12 | num -= iAryKeys[i]; 13 | } 14 | } 15 | return strResult; 16 | } 17 | }; -------------------------------------------------------------------------------- /58. length-of-last-word/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* reverseList(ListNode* head) { 12 | ListNode* prev = nullptr; 13 | ListNode* curr = head; 14 | while (curr) { 15 | ListNode* next = curr->next; 16 | curr->next = prev; 17 | prev = curr; 18 | curr = next; 19 | } 20 | return prev; 21 | } 22 | }; -------------------------------------------------------------------------------- /303. range-sum-query-immutable/main.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | NumArray(vector& nums) { 4 | _nums = nums; 5 | } 6 | 7 | int sumRange(int i, int j) { 8 | int _iResult = 0; 9 | for(int index = i; index <= j; index++) { 10 | _iResult += _nums[index]; 11 | } 12 | return _iResult; 13 | } 14 | private: 15 | vector _nums; 16 | 17 | }; 18 | 19 | /** 20 | * Your NumArray object will be instantiated and called as such: 21 | * NumArray* obj = new NumArray(nums); 22 | * int param_1 = obj->sumRange(i,j); 23 | */ 24 | -------------------------------------------------------------------------------- /1603. design-parking-system/main.cpp: -------------------------------------------------------------------------------- 1 | class ParkingSystem { 2 | public: 3 | ParkingSystem(int big, int medium, int small) { 4 | _iArrayRoom[0] = big; 5 | _iArrayRoom[1] = medium; 6 | _iArrayRoom[2] = small; 7 | } 8 | 9 | bool addCar(int carType) { 10 | return _iArrayRoom[carType - 1]-- > 0; 11 | } 12 | 13 | private: 14 | int _iArrayRoom[3]; 15 | }; 16 | 17 | /** 18 | * Your ParkingSystem object will be instantiated and called as such: 19 | * ParkingSystem* obj = new ParkingSystem(big, medium, small); 20 | * bool param_1 = obj->addCar(carType); 21 | */ -------------------------------------------------------------------------------- /5. longest-palindromic-substring/README.md: -------------------------------------------------------------------------------- 1 | # 5. longest-palindromic-substring 2 | 3 | [原题链接](https://leetcode-cn.com/problems/longest-palindromic-substring/) 4 | 5 | `动态规划` 6 | 本季刷题接触到的陌生知识(虽然这名字早已如雷贯耳) 7 | 看了看动态规划的题解想到了可以用到扩展算法(不愧是我!虽然不知道是不是下面介绍的中心扩展算法) 8 | 先写一下试试~ 9 | 10 | 雾草,成了! 11 | 运行时间大概是官方动态规划解法的 1/21,内存占用是其 1/8 12 | 牛蛙牛蛙~ 13 | 14 | ### 解题思路 15 | 16 | 回文串可以是偶数长度和奇数长度 17 | 18 | + 奇数长度的回文串可以遍历每个字符串,然后由短到长向两侧延伸 19 | + 偶数长度的回文串同理,也是从中心向两侧扩展,但是偶数长度的字符串起始位置必然是两个连续的相同字符所在的位置 20 | + 综上所述,只需要分类找出以上两种字符串并在最后找到长度最长的子串并返回就可以了 21 | 22 | ### 代码展示 23 | 24 | 见 `main.cpp` 源码文件 25 | 26 | > 2021 - 03 - 05 -------------------------------------------------------------------------------- /50. powx-n/README.md: -------------------------------------------------------------------------------- 1 | # 50. powx-n 2 | 3 | [原题链接](https://leetcode-cn.com/problems/powx-n/) 4 | 5 | 果然中等难度的题目没有那么简单…… 6 | 我歇逼了,想破脑子顶多能搞出下面的代码: 7 | 8 | ```c++ 9 | class Solution { 10 | public: 11 | double myPow(double x, int n) { 12 | if (x == 0) return 0; 13 | if (x == 1 || n == 0) return 1; 14 | double dResult = 1; 15 | for (int i = 0; i < abs(n); i++) 16 | dResult *= x; 17 | return n > 0 ? dResult : 1.0 / dResult; 18 | } 19 | }; 20 | ``` 21 | 22 | 先去不讲武德一次,然后再去认真研究题解吧…… 23 | 24 | 好家伙,我直接调用 `pow()` 通过了后才发现这不是每日一题…… 25 | 26 | > 2021 - 03 - 27 27 | -------------------------------------------------------------------------------- /剑指 Offer 24. fan-zhuan-lian-biao-lcof/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* reverseList(ListNode* head) { 12 | ListNode* prev = nullptr; 13 | ListNode* curr = head; 14 | while (curr) { 15 | ListNode* next = curr->next; 16 | curr->next = prev; 17 | prev = curr; 18 | curr = next; 19 | } 20 | return prev; 21 | } 22 | }; -------------------------------------------------------------------------------- /169. majority-element/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | int iLength = nums.size(); 5 | if (iLength == 1 || iLength == 2) return nums[0]; 6 | sort(nums.begin(), nums.end()); 7 | int iRightCount = iLength / 2, iIndex = 0; 8 | while (iIndex < iLength) { 9 | int _iNum = nums[iIndex], _iCount = 1; 10 | while (iIndex < iLength && _iNum == nums[++iIndex]) 11 | _iCount++; 12 | if (_iCount > iRightCount) return _iNum; 13 | } 14 | return nums[iIndex]; 15 | } 16 | }; -------------------------------------------------------------------------------- /456. 132-pattern/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | int iLength = nums.size(), iMaxK = INT_MIN; 5 | stack stkCandidateK; 6 | stkCandidateK.push(nums[iLength - 1]); 7 | for (int i = iLength - 2; i >= 0; i--) { 8 | if (nums[i] < iMaxK) return true; 9 | while (!stkCandidateK.empty() && nums[i] > stkCandidateK.top()) { 10 | iMaxK = stkCandidateK.top(); stkCandidateK.pop(); 11 | } 12 | if (nums[i] > iMaxK) stkCandidateK.push(nums[i]); 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /331. verify-preorder-serialization-of-a-binary-tree/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValidSerialization(string preorder) { 4 | int iLength = preorder.size(), iIndex = 0, iNumSolts = 1; 5 | while (iIndex < iLength) { 6 | if (iNumSolts == 0) return false; 7 | switch (preorder[iIndex]) { 8 | case ',': iIndex++; break; 9 | case '#': iNumSolts--, iIndex++; break; 10 | default: while (iIndex < iLength && preorder[iIndex] != ',') iIndex++; iNumSolts++; // iNumSolts - 1 + 2 11 | } 12 | } 13 | return iNumSolts == 0; 14 | } 15 | }; -------------------------------------------------------------------------------- /155. min-stack/main.py: -------------------------------------------------------------------------------- 1 | class MinStack: 2 | 3 | def __init__(self): 4 | """ 5 | initialize your data structure here. 6 | """ 7 | self.stack = [] 8 | 9 | def push(self, val: int) -> None: 10 | self.stack.append(val) 11 | 12 | def pop(self) -> None: 13 | self.stack.pop() 14 | 15 | def top(self) -> int: 16 | return self.stack[-1] 17 | 18 | def getMin(self) -> int: 19 | return min(self.stack) 20 | 21 | 22 | # Your MinStack object will be instantiated and called as such: 23 | # obj = MinStack() 24 | # obj.push(val) 25 | # obj.pop() 26 | # param_3 = obj.top() 27 | # param_4 = obj.getMin() -------------------------------------------------------------------------------- /面试题 17.21. volume-of-histogram-lcci/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int iSum = accumulate(height.begin(), height.end(), 0); 5 | int iVolume = 0, iIndexLayer = 1, iIndexLeft = 0, iIndexRight = height.size() - 1; 6 | while (iIndexLeft <= iIndexRight) { 7 | while (iIndexLeft <= iIndexRight && height[iIndexLeft] < iIndexLayer) iIndexLeft++; 8 | while (iIndexLeft <= iIndexRight && height[iIndexRight] < iIndexLayer) iIndexRight--; 9 | iVolume += iIndexRight - iIndexLeft + 1; 10 | iIndexLayer++; 11 | } 12 | return iVolume - iSum; 13 | } 14 | }; -------------------------------------------------------------------------------- /3. longest-substring-without-repeating-characters/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | unordered_set usCharList; 5 | int iStrLength = s.size(), iIndexRightPoint = -1, iResult = 0; 6 | for(int i = 0; i < iStrLength; i++) { 7 | if (i != 0) usCharList.erase(s[i - 1]); 8 | while(iIndexRightPoint + 1 < iStrLength && !usCharList.count(s[iIndexRightPoint + 1])) { 9 | usCharList.insert(s[iIndexRightPoint + 1]); 10 | iIndexRightPoint++; 11 | } 12 | iResult = max(iResult, iIndexRightPoint - i + 1); 13 | } 14 | return iResult; 15 | } 16 | }; -------------------------------------------------------------------------------- /38. count-and-say/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | string strResult = "1"; 5 | for (int i = 0; i < n - 1; i++) 6 | strResult = GenerateNext(strResult); 7 | return strResult; 8 | } 9 | private: 10 | string GenerateNext(string str) { 11 | string strResult; 12 | int iIndex = 0, iLength = str.size(); 13 | while (iIndex < iLength) { 14 | int _iNum = 1; 15 | while (iIndex + _iNum < iLength && str[iIndex] == str[iIndex + _iNum]) _iNum++; 16 | strResult += to_string(_iNum)+ str[iIndex]; 17 | iIndex += _iNum; 18 | } 19 | return strResult; 20 | } 21 | }; -------------------------------------------------------------------------------- /1. two-sum/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | vector vResults; 5 | int iLengthNums = nums.size(); 6 | bool bIsFoundResult = false; 7 | for (int i = 0; (i < iLengthNums) && !bIsFoundResult; i++) { 8 | int _iAnotherResult = target - nums[i]; 9 | for (int j = i + 1; j < iLengthNums; j++) { 10 | if (nums[j] == _iAnotherResult) { 11 | vResults.push_back(i); 12 | vResults.push_back(j); 13 | bIsFoundResult = true; 14 | break; 15 | } 16 | } 17 | } 18 | return vResults; 19 | } 20 | }; -------------------------------------------------------------------------------- /1006. clumsy-factorial/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int clumsy(int N) { 4 | int iResult = 0; 5 | if (N >= 4) iResult = N * (N - 1) / (N - 2) + (N - 3); 6 | for (int i = 1; i < N / 4; i++) 7 | iResult -= (N - 4 * i) * (N - 4 * i - 1) / (N - 4 * i - 2) - (N - 4 * i - 3); 8 | switch(N % 4) { 9 | case 0: 10 | break; 11 | case 1: 12 | iResult -= 1; 13 | break; 14 | case 2: 15 | iResult -= 2 * 1; 16 | break; 17 | case 3: 18 | iResult -= 3 * 2 / 1; 19 | break; 20 | } 21 | return N < 4 ? -iResult : iResult; 22 | } 23 | }; -------------------------------------------------------------------------------- /191. number-of-1-bits/README.md: -------------------------------------------------------------------------------- 1 | # 191. number-of-1-bits 2 | 3 | [原题链接](https://leetcode-cn.com/problems/number-of-1-bits/) 4 | 5 | 怠惰了,怠惰了……这几天每日一混昨天还忘了刷题 6 | 今日每日一题是简单难度,看到题解里面提到了 `汉明重量` 7 | 还未来得及仔细读题,便先跳到 [百度百科](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F) 去科普一下 8 | 看到百科里面有一段代码示例便复制过来运行了一下(虽然都还没仔细看) 9 | 好家伙,直接 100% + 96.11% ,无语……稍后自己仔细做一做见第二版代码 10 | 11 | 位运算永远的痛……找个机会恶补一下,溜了…… 12 | 13 | 不过 Python 的代码是真滴简单,简单到离谱(如果不讲武德不考虑效率的话) 14 | 15 | 好家伙,又看到一个拿 [Java 一行写完的版本](https://leetcode-cn.com/problems/number-of-1-bits/solution/qiao-yong-wei-yun-suan-zhi-xu-ji-xing-da-iga5/),不试一下岂不是亏了 16 | 17 | 之前看到一个题解讲了 Java 里面 `>>` 和 `>>>` 的坑 18 | 突然想这个一行版本的代码会不会也有类似的坑,于是又拿 C# 试了一下…… 19 | (直接为自己刷项目语言找借口) 20 | 21 | > 2020 - 03 - 22 22 | -------------------------------------------------------------------------------- /10. regular-expression-matching/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int m = s.size() + 1, n = p.size() + 1; 5 | vector> dp(m, vector(n, false)); 6 | dp[0][0] = true; 7 | for(int j = 2; j < n; j += 2) 8 | dp[0][j] = dp[0][j - 2] && p[j - 1] == '*'; 9 | for(int i = 1; i < m; i++) { 10 | for(int j = 1; j < n; j++) { 11 | dp[i][j] = p[j - 1] == '*' ? 12 | dp[i][j - 2] || dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'): 13 | dp[i - 1][j - 1] && (p[j - 1] == '.' || s[i - 1] == p[j - 1]); 14 | } 15 | } 16 | return dp[m - 1][n - 1]; 17 | } 18 | }; -------------------------------------------------------------------------------- /83. remove-duplicates-from-sorted-list/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | if (!head) return head; 15 | ListNode* pRawHead = head; 16 | while (head->next) 17 | if (head->val == head->next->val) 18 | head->next = head->next->next; 19 | else 20 | head = head->next; 21 | return pRawHead; 22 | } 23 | }; -------------------------------------------------------------------------------- /101. symmetric-tree/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isSymmetric(TreeNode* root) { 15 | return check(root, root); 16 | } 17 | 18 | private: 19 | bool check(TreeNode* pLeft, TreeNode* pRight) { 20 | return !pLeft && !pRight ? true : (!pLeft || !pRight ? false : pLeft->val == pRight->val && check(pLeft->left, pRight->right) && check(pLeft->right, pRight->left)); 21 | } 22 | }; -------------------------------------------------------------------------------- /206. reverse-linked-list/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseList(ListNode* head) { 14 | if (!head) return head; 15 | vector vPNodes; 16 | while(head) { 17 | vPNodes.push_back(head); 18 | head = head->next; 19 | } 20 | for (int i = vPNodes.size() - 1; i > 0; i--) 21 | vPNodes[i]->next = vPNodes[i - 1]; 22 | vPNodes[0]->next = nullptr; 23 | return vPNodes[vPNodes.size() - 1]; 24 | } 25 | }; -------------------------------------------------------------------------------- /16. 3sum-closest/README.md: -------------------------------------------------------------------------------- 1 | # 16. 3sum-closest 2 | 3 | [原题链接](https://leetcode-cn.com/problems/3sum-closest/) 4 | 5 | 昨天刚做完三数之和那道题便看到了这道题 6 | 直到刚才还在以为只需要稍微变一下就可以完成这道题啦 7 | 甚至这道题比昨天的三数之和更简单 8 | 在动手修改代码时才发现由于条件宽泛了(由相等变成了更接近) 9 | 题目反而更加困难了,继续沿用昨天的代码逻辑会更加混杂 10 | 无奈只好再次求助于题解…… 11 | 12 | 可是,为什么这道题这么简单?! 13 | 果然有的时候,经验反而会是阻碍自己思考的障碍 14 | 15 | 淦,我说怎么差别这么大 16 | 原来[这位大佬的代码](https://leetcode-cn.com/problems/3sum-closest/solution/hua-jie-suan-fa-16-zui-jie-jin-de-san-shu-zhi-he-b/)没有考虑去重的问题 17 | 虽然也不是不能用,但是总感觉能考虑到的东西没有处理好很不舒服 18 | 稍后再来一份优化后的代码吧 19 | 20 | 首先是 8ms 和 93.62% 的打败让我意识到似乎不去重也没有太大问题 21 | 然后翻了一下另一版去重答案提供者的评论区 22 | 发现大家对于去重的意见也是各异 23 | 仔细考虑一下,类似于 `nums = [1, 1, 1, 2, 3] ,target = 3` 的这种情况 24 | 最佳答案就应该是三个连续的 1 ,去重反而会让问题变得更复杂 25 | 在力扣OJ这种测试数据量并不是十分惊人的情况下,感觉还是可以不去重的 26 | 27 | > 2021 - 03 - 09 28 | -------------------------------------------------------------------------------- /16. 3sum-closest/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& nums, int target) { 4 | sort(nums.begin(), nums.end()); 5 | int iLengthNums = nums.size(), iResult = nums[0] + nums[1] + nums[2]; 6 | for (int i = 0; i < iLengthNums; i++) { 7 | int _iIndexLeft = i + 1, _iIndexRight = iLengthNums - 1; 8 | while (_iIndexLeft < _iIndexRight) { 9 | int _iSum = nums[i] + nums[_iIndexLeft] + nums[_iIndexRight]; 10 | if (abs(target - iResult) > abs(target - _iSum)) iResult = _iSum; 11 | if (_iSum == target) 12 | return iResult; 13 | else 14 | _iSum > target ? _iIndexRight-- : _iIndexLeft++; 15 | } 16 | } 17 | return iResult; 18 | } 19 | }; -------------------------------------------------------------------------------- /705. design-hashset/main.cpp: -------------------------------------------------------------------------------- 1 | class MyHashSet { 2 | public: 3 | /** Initialize your data structure here. */ 4 | MyHashSet() { 5 | 6 | } 7 | 8 | void add(int key) { 9 | _hashmapValues.insert(pair(key, ' ')); 10 | } 11 | 12 | void remove(int key) { 13 | _hashmapValues.erase(key); 14 | } 15 | 16 | /** Returns true if this set contains the specified element */ 17 | bool contains(int key) { 18 | return _hashmapValues.find(key) != _hashmapValues.end(); 19 | } 20 | private: 21 | unordered_map _hashmapValues; 22 | 23 | }; 24 | 25 | /** 26 | * Your MyHashSet object will be instantiated and called as such: 27 | * MyHashSet* obj = new MyHashSet(); 28 | * obj->add(key); 29 | * obj->remove(key); 30 | * bool param_3 = obj->contains(key); 31 | */ -------------------------------------------------------------------------------- /19. remove-nth-node-from-end-of-list/main_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* removeNthFromEnd(ListNode* head, int n) { 14 | ListNode* pDummy = new ListNode(0, head), * pFirst = head, * pSecond = pDummy; 15 | for (int i = 0; i < n; i++) pFirst = pFirst->next; 16 | while (pFirst) pFirst = pFirst->next, pSecond = pSecond->next; 17 | pSecond->next = pSecond->next->next; 18 | ListNode* pResult = pDummy->next; 19 | delete(pDummy); 20 | return pResult; 21 | } 22 | }; -------------------------------------------------------------------------------- /19. remove-nth-node-from-end-of-list/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* removeNthFromEnd(ListNode* head, int n) { 14 | ListNode* aryNodes[30] = { nullptr }; 15 | int iNumNode = 0; 16 | while (head) { 17 | aryNodes[iNumNode] = head; 18 | head = head->next; 19 | iNumNode++; 20 | } 21 | if (iNumNode == n) return aryNodes[1]; 22 | aryNodes[iNumNode - n - 1]->next = aryNodes[iNumNode - n + 1]; 23 | return aryNodes[0]; 24 | } 25 | }; -------------------------------------------------------------------------------- /234. palindrome-linked-list/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | bool isPalindrome(ListNode* head) { 14 | if (!head) return head; 15 | vector vPNodes; 16 | while (head) { 17 | vPNodes.push_back(head); 18 | head = head->next; 19 | } 20 | int iLength = vPNodes.size(); 21 | for(int i = 0; i < iLength / 2; i++) 22 | if (vPNodes[i]->val != vPNodes[iLength - i - 1]->val) 23 | return false; 24 | return true; 25 | } 26 | }; -------------------------------------------------------------------------------- /155. min-stack/main_1.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | public: 3 | /** initialize your data structure here. */ 4 | MinStack() { 5 | _stkMin.push(INT_MAX); 6 | } 7 | 8 | void push(int val) { 9 | _stk.push(val); 10 | _stkMin.push(min(_stkMin.top(), val)); 11 | } 12 | 13 | void pop() { 14 | _stk.pop(); 15 | _stkMin.pop(); 16 | } 17 | 18 | int top() { 19 | return _stk.top(); 20 | } 21 | 22 | int getMin() { 23 | return _stkMin.top(); 24 | } 25 | 26 | private: 27 | stack _stk; 28 | stack _stkMin; 29 | }; 30 | 31 | /** 32 | * Your MinStack object will be instantiated and called as such: 33 | * MinStack* obj = new MinStack(); 34 | * obj->push(val); 35 | * obj->pop(); 36 | * int param_3 = obj->top(); 37 | * int param_4 = obj->getMin(); 38 | */ -------------------------------------------------------------------------------- /155. min-stack/main.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | public: 3 | /** initialize your data structure here. */ 4 | MinStack() { 5 | 6 | } 7 | 8 | void push(int val) { 9 | _stkNums.push_back(val); 10 | } 11 | 12 | void pop() { 13 | _stkNums.pop_back(); 14 | } 15 | 16 | int top() { 17 | return _stkNums.back(); 18 | } 19 | 20 | int getMin() { 21 | int iMinNum = _stkNums[0]; 22 | for (int num : _stkNums) 23 | if (num < iMinNum) iMinNum = num; 24 | return iMinNum; 25 | } 26 | private: 27 | vector _stkNums; 28 | }; 29 | 30 | /** 31 | * Your MinStack object will be instantiated and called as such: 32 | * MinStack* obj = new MinStack(); 33 | * obj->push(val); 34 | * obj->pop(); 35 | * int param_3 = obj->top(); 36 | * int param_4 = obj->getMin(); 37 | */ -------------------------------------------------------------------------------- /10. regular-expression-matching/README.md: -------------------------------------------------------------------------------- 1 | # 10. regular-expression-matching 2 | 3 | [原题链接](https://leetcode-cn.com/problems/regular-expression-matching/) 4 | 5 | 再次不讲武德,罪过罪过…… 6 | 直接使用 `regex_match` 虽然香,但是…… 7 | 我执行用时只打败了 13.16% 的用户,内存消耗只打败 19.29% 的用户 8 | 这群人是什么妖魔鬼怪? 9 | (我要默默刷题,然后卷死所有人……) 10 | 11 | 不得不说,这是我这一直以来第一次挑战 `困难` 难度的题目 12 | 就算是为了尊重他一下,也要学习一下大佬们的写法吧 13 | 稍后再更新一版新的的解题思路(如果这个文件夹下存在 `main_1.cpp` 的话) 14 | 15 | 我裂开了,看了 **一下午 + 一晚上** 愣是没完全搞懂[那个大佬的动态规划算法](https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/solution/jian-zhi-offer-19-zheng-ze-biao-da-shi-pi-pei-dong/) 16 | (虽然貌似很多人是看官方题解不懂看他的才看明白的,是我太菜了……) 17 | 好不容易看懂了一半心想另一半抄完得了,不完全理解就先背过解题思路 18 | 谁知抄都抄错了!自己到现在还不知道哪里出了问题是最离谱的! 19 | 总之就是完全复制大佬的代码覆盖自己的之后,运行成功了 20 | (运行时间 0ms 打败 100% 的用户是最离谱的……我直接跪倒) 21 | 22 | 一天下来只搞了这一个题,挫败感极强 23 | 24 | 另外:傻x体测 25 | 26 | > 2021 - 03 - 06 ~ 2021 - 03 - 07 27 | -------------------------------------------------------------------------------- /8. string-to-integer-atoi/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string s) { 4 | int iIndex = 0, iLengthStr = s.size(), iResult = 0; 5 | bool bIsNegative = false; 6 | while (s[iIndex] == ' ') iIndex++; 7 | if (iIndex < iLengthStr) { 8 | if (s[iIndex] == '-') bIsNegative = true, iIndex++; 9 | else if (s[iIndex] == '+') iIndex++; 10 | } 11 | while (iIndex < iLengthStr && s[iIndex] >= '0' && s[iIndex] <= '9') { 12 | int _iNum = bIsNegative ? '0' - s[iIndex] : s[iIndex] - '0'; 13 | if (iResult > INT_MAX / 10 || (iResult == INT_MAX / 10 && _iNum > 7)) return INT_MAX; 14 | if (iResult < INT_MIN / 10 || (iResult == INT_MIN / 10 && _iNum < -8)) return INT_MIN; 15 | iResult = iResult * 10 + _iNum, iIndex++; 16 | } 17 | return iResult; 18 | } 19 | }; -------------------------------------------------------------------------------- /15. 3sum/README.md: -------------------------------------------------------------------------------- 1 | # 15. 3sum 2 | 3 | [原题链接](https://leetcode-cn.com/problems/3sum/) 4 | 5 | 刚要说有手就行,却在动笔时突然想到了昨天被最多装水题超时所支配的恐惧 6 | 想要楞头写下去却在调试时发现自己没有对数组进行去重…… 7 | 淦啊,再加上去重就是耶稣来了也得超时啊! 8 | 无语……再次求助大佬们的题解 9 | 在新建本题文件夹时蓦然瞥见 4sum 的代码 10 | 苦不堪言.jpg 11 | 12 | 以下为 暴力 + 未去重 的(未完成)代码 13 | ```c++ 14 | class Solution { 15 | public: 16 | vector> threeSum(vector& nums) { 17 | vector> vResults; 18 | int iLengthNums = nums.size(); 19 | for (int i = 0; i < iLengthNums; i++) 20 | for (int j = i + 1; j < iLengthNums; j++) 21 | for (int k = j + 1; k < iLengthNums; k++) 22 | if (!(nums[i] + nums[j] + nums[k])) 23 | vResults.push_back(vector({nums[i], nums[j], nums[k]})); 24 | return vResults; 25 | } 26 | }; 27 | ``` 28 | 29 | 不得不说,双指针这种遍历方式是针不戳 30 | 31 | > 2021 - 03 - 08 32 | -------------------------------------------------------------------------------- /20. valid-parentheses/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stack stk; 5 | 6 | for (auto c : s) 7 | { 8 | if (c == '[') 9 | { 10 | stk.push(']'); 11 | } 12 | else if (c == '(') 13 | { 14 | stk.push(')'); 15 | } 16 | else if (c == '{') 17 | { 18 | stk.push('}'); 19 | } 20 | else 21 | { 22 | if (stk.empty()) 23 | { 24 | return false; 25 | } 26 | if (c != stk.top()) 27 | { 28 | return false; 29 | } 30 | stk.pop(); 31 | } 32 | 33 | 34 | } 35 | return stk.empty(); 36 | } 37 | }; -------------------------------------------------------------------------------- /705. design-hashset/main_1.cpp: -------------------------------------------------------------------------------- 1 | class MyHashSet { 2 | public: 3 | /** Initialize your data structure here. */ 4 | MyHashSet() { 5 | 6 | } 7 | 8 | void add(int key) { 9 | if (!contains(key)) _vValues.push_back(key); 10 | } 11 | 12 | void remove(int key) { 13 | _vValues.erase(std::remove(_vValues.begin(), _vValues.end(), key), _vValues.end()); 14 | } 15 | 16 | /** Returns true if this set contains the specified element */ 17 | bool contains(int key) { 18 | for (int value : _vValues) 19 | if (key == value) return true; 20 | return false; 21 | } 22 | private: 23 | vector _vValues; 24 | 25 | }; 26 | 27 | /** 28 | * Your MyHashSet object will be instantiated and called as such: 29 | * MyHashSet* obj = new MyHashSet(); 30 | * obj->add(key); 31 | * obj->remove(key); 32 | * bool param_3 = obj->contains(key); 33 | */ -------------------------------------------------------------------------------- /11. container-with-most-water/README.md: -------------------------------------------------------------------------------- 1 | # 11. container-with-most-water 2 | 3 | [原题链接](https://leetcode-cn.com/problems/container-with-most-water/) 4 | 5 | 刚拿过来便被那个 `"你不能倾斜容器"` 的说明给笑到了 6 | 哈哈哈哈哈倾斜容器来盛更多的水,这是碳基生物能想到的吗(指在算法题中 7 | 但是开始思考的时候才发现,淦,这才是难度增加的关键 8 | 9 | 但是也不是很难,于是动手写了第一版代码 10 | 11 | ```c++ 12 | class Solution { 13 | public: 14 | int maxArea(vector& height) { 15 | int iMaxArea = 0, iLengthPoints = height.size(); 16 | for (int i = 0; i < iLengthPoints; i++) { 17 | for (int j = i + 1; j < iLengthPoints; j++) { 18 | int _iArea = (j - i) * min(height[i], height[j]); 19 | if (_iArea > iMaxArea) iMaxArea = _iArea; 20 | } 21 | } 22 | return iMaxArea; 23 | } 24 | }; 25 | ``` 26 | 27 | 没有什么是暴力解决不了的 28 | 如果有的话,那就是…… 29 | 超时了 NMD ! 30 | 我直接裂开去查题解 31 | `双指针算法` 32 | 终于搞定了,太淦了…… 33 | 34 | > 2021 - 03 - 07 35 | -------------------------------------------------------------------------------- /503. next-greater-element-ii/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElements(vector& nums) { 4 | vector iResults; 5 | int iNumCount = nums.size(); 6 | for (int iIndexNum = 0; iIndexNum < iNumCount; iIndexNum++) { 7 | int _iIndexNextNum = (iIndexNum + 1) % iNumCount; 8 | bool _isFindNextLargerNum = false; 9 | while (_iIndexNextNum != iIndexNum) { 10 | if (nums[_iIndexNextNum] > nums[iIndexNum]) { 11 | iResults.push_back(nums[_iIndexNextNum]); 12 | _isFindNextLargerNum = true; 13 | break; 14 | } else { 15 | _iIndexNextNum = ++_iIndexNextNum % iNumCount; 16 | } 17 | } 18 | if (!_isFindNextLargerNum) iResults.push_back(-1); 19 | } 20 | return iResults; 21 | } 22 | }; -------------------------------------------------------------------------------- /208. implement-trie-prefix-tree/main.java: -------------------------------------------------------------------------------- 1 | class Trie { 2 | 3 | String data = ","; 4 | 5 | /** Initialize your data structure here. */ 6 | public Trie() { 7 | 8 | } 9 | 10 | /** Inserts a word into the trie. */ 11 | public void insert(String word) { 12 | data += word + ","; 13 | } 14 | 15 | /** Returns if the word is in the trie. */ 16 | public boolean search(String word) { 17 | return data.contains("," + word + ","); 18 | } 19 | 20 | /** Returns if there is any word in the trie that starts with the given prefix. */ 21 | public boolean startsWith(String prefix) { 22 | return data.contains("," + prefix); 23 | } 24 | } 25 | 26 | /** 27 | * Your Trie object will be instantiated and called as such: 28 | * Trie obj = new Trie(); 29 | * obj.insert(word); 30 | * boolean param_2 = obj.search(word); 31 | * boolean param_3 = obj.startsWith(prefix); 32 | */ -------------------------------------------------------------------------------- /61. rotate-list/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* rotateRight(ListNode* head, int k) { 14 | if (!k || !head) return head; 15 | ListNode* pRawHead = head; 16 | vector vPListNode; 17 | while (head) { 18 | vPListNode.push_back(head); 19 | head = head->next; 20 | } 21 | int iLength = vPListNode.size(); 22 | if (!(k % iLength)) return pRawHead; 23 | vPListNode[iLength - 1]->next = pRawHead; 24 | vPListNode[iLength - k % iLength - 1]->next = nullptr; 25 | return vPListNode[iLength - k % iLength]; 26 | } 27 | }; -------------------------------------------------------------------------------- /227. basic-calculator-ii/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calculate(string s) { 4 | vector vStkValues; 5 | char chPreSign = '+'; 6 | int iValue = 0, iLengthStr = s.size(); 7 | for (int i = 0; i < iLengthStr; i++) { 8 | if (isdigit(s[i])) 9 | iValue = iValue * 10 + int(s[i] - '0'); 10 | if (!isdigit(s[i]) && s[i] != ' ' || i == iLengthStr - 1) { 11 | switch (chPreSign) { 12 | case '+': vStkValues.push_back(iValue); break; 13 | case '-': vStkValues.push_back(-iValue); break; 14 | case '*': vStkValues.back() *= iValue; break; 15 | case '/': vStkValues.back() /= iValue; break; 16 | } 17 | chPreSign = s[i], iValue = 0; 18 | } 19 | } 20 | return accumulate(vStkValues.begin(), vStkValues.end(), 0); 21 | } 22 | }; -------------------------------------------------------------------------------- /82. remove-duplicates-from-sorted-list-ii/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | ListNode* dummy = new ListNode(-1, head); 15 | ListNode* slow = nullptr, *fast = dummy; 16 | while (fast) { 17 | slow = fast, fast = fast->next; 18 | while (fast && fast->next && fast->val == fast->next->val) { 19 | int repeat_num = fast->val; 20 | while (fast && fast->val == repeat_num) 21 | fast = fast->next; 22 | slow->next = fast; 23 | } 24 | } 25 | return dummy->next; 26 | } 27 | }; -------------------------------------------------------------------------------- /54. spiral-matrix/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector spiralOrder(vector>& matrix) { 4 | vector vResult; 5 | int iLimitUp = 0, iLimitDown = matrix.size() - 1, iLimitLeft = 0, iLimitRight = matrix[0].size() - 1; 6 | while (true) { 7 | for (int i = iLimitLeft; i <= iLimitRight; i++) vResult.push_back(matrix[iLimitUp][i]); 8 | if (++iLimitUp > iLimitDown) break; 9 | for (int i = iLimitUp; i <= iLimitDown; i++) vResult.push_back(matrix[i][iLimitRight]); 10 | if (--iLimitRight < iLimitLeft) break; 11 | for (int i = iLimitRight; i >= iLimitLeft; i--) vResult.push_back(matrix[iLimitDown][i]); 12 | if (--iLimitDown < iLimitUp) break; 13 | for (int i = iLimitDown; i >= iLimitUp; i--) vResult.push_back(matrix[i][iLimitLeft]); 14 | if (++iLimitLeft > iLimitRight) break; 15 | } 16 | return vResult; 17 | } 18 | }; -------------------------------------------------------------------------------- /705. design-hashset/main.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Initialize your data structure here. 3 | */ 4 | var MyHashSet = function() { 5 | this.hash_table = {}; 6 | }; 7 | 8 | /** 9 | * @param {number} key 10 | * @return {void} 11 | */ 12 | MyHashSet.prototype.add = function(key) { 13 | this.hash_table[key] = true; 14 | }; 15 | 16 | /** 17 | * @param {number} key 18 | * @return {void} 19 | */ 20 | MyHashSet.prototype.remove = function(key) { 21 | delete this.hash_table[key]; 22 | // 或者:this.hash_table[key] = false; 23 | }; 24 | 25 | /** 26 | * Returns true if this set contains the specified element 27 | * @param {number} key 28 | * @return {boolean} 29 | */ 30 | MyHashSet.prototype.contains = function(key) { 31 | return Boolean(this.hash_table[key]); 32 | }; 33 | 34 | /** 35 | * Your MyHashSet object will be instantiated and called as such: 36 | * var obj = new MyHashSet() 37 | * obj.add(key) 38 | * obj.remove(key) 39 | * var param_3 = obj.contains(key) 40 | */ -------------------------------------------------------------------------------- /3. longest-substring-without-repeating-characters/README.md: -------------------------------------------------------------------------------- 1 | # 3. longest-substring-without-repeating-characters 2 | 3 | [原题链接](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) 4 | 5 | 今日感悟:***官方题解就是香*** 6 | 7 | 什么?我竟然写出了卡死力扣OJ的代码? 8 | (不是卡死,只是判题时间太长了……足足一分多钟) 9 | (可能是网络问题,但是难得一见所以记录在下面) 10 | 11 | ```c++ 12 | class Solution { 13 | public: 14 | int lengthOfLongestSubstring(string s) { 15 | unordered_set usCharList; 16 | int iStrLength = s.size(), iIndexRightPoint = -1, iResult = 0; 17 | for(int i = 0; i < iStrLength; i++) { 18 | if (i != 0) s.erase(s[i - 1]); 19 | while(iIndexRightPoint + 1 < iStrLength && !usCharList.count(s[iIndexRightPoint + 1])) { 20 | usCharList.insert(s[iIndexRightPoint + 1]); 21 | iIndexRightPoint++; 22 | } 23 | iResult = max(iResult, iIndexRightPoint - i + 1); 24 | } 25 | return iResult; 26 | } 27 | }; 28 | ``` 29 | 30 | > 2021 - 03 - 04 -------------------------------------------------------------------------------- /232. implement-queue-using-stacks/main.cpp: -------------------------------------------------------------------------------- 1 | class MyQueue { 2 | public: 3 | /** Initialize your data structure here. */ 4 | MyQueue() { 5 | 6 | } 7 | 8 | /** Push element x to the back of queue. */ 9 | void push(int x) { 10 | _vNumList.push_back(x); 11 | } 12 | 13 | /** Removes the element from in front of queue and returns that element. */ 14 | int pop() { 15 | int iResult = _vNumList[0]; 16 | _vNumList.erase(_vNumList.begin()); 17 | return iResult; 18 | } 19 | 20 | /** Get the front element. */ 21 | int peek() { 22 | return _vNumList[0]; 23 | } 24 | 25 | /** Returns whether the queue is empty. */ 26 | bool empty() { 27 | return _vNumList.empty(); 28 | } 29 | private: 30 | vector _vNumList; 31 | }; 32 | 33 | /** 34 | * Your MyQueue object will be instantiated and called as such: 35 | * MyQueue* obj = new MyQueue(); 36 | * obj->push(x); 37 | * int param_2 = obj->pop(); 38 | * int param_3 = obj->peek(); 39 | * bool param_4 = obj->empty(); 40 | */ -------------------------------------------------------------------------------- /21. merge-two-sorted-lists/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 14 | vector vNodes; 15 | while (l1) { 16 | vNodes.push_back(l1); 17 | l1 = l1->next; 18 | } 19 | while (l2) { 20 | vNodes.push_back(l2); 21 | l2 = l2->next; 22 | } 23 | sort(vNodes.begin(), vNodes.end(), Sort); 24 | vNodes.push_back(nullptr); 25 | for (int i = 0; i < vNodes.size() - 1; i++) 26 | vNodes[i]->next = vNodes[i + 1]; 27 | return vNodes[0]; 28 | } 29 | 30 | private: 31 | static bool Sort(const ListNode* pNode1, const ListNode* pNode2) { 32 | return pNode1->val < pNode2->val; 33 | } 34 | }; -------------------------------------------------------------------------------- /208. implement-trie-prefix-tree/main.cpp: -------------------------------------------------------------------------------- 1 | class Trie { 2 | public: 3 | /** Initialize your data structure here. */ 4 | Trie() { 5 | 6 | } 7 | 8 | /** Inserts a word into the trie. */ 9 | void insert(string word) { 10 | _vStrWords.push_back(word); 11 | } 12 | 13 | /** Returns if the word is in the trie. */ 14 | bool search(string word) { 15 | for (string str : _vStrWords) 16 | if (str == word) return true; 17 | return false; 18 | } 19 | 20 | /** Returns if there is any word in the trie that starts with the given prefix. */ 21 | bool startsWith(string prefix) { 22 | for (string str : _vStrWords) 23 | if (str.substr(0, prefix.size()) == prefix) return true; 24 | return false; 25 | } 26 | 27 | private: 28 | vector _vStrWords; 29 | }; 30 | 31 | /** 32 | * Your Trie object will be instantiated and called as such: 33 | * Trie* obj = new Trie(); 34 | * obj->insert(word); 35 | * bool param_2 = obj->search(word); 36 | * bool param_3 = obj->startsWith(prefix); 37 | */ -------------------------------------------------------------------------------- /17. letter-combinations-of-a-phone-number/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector letterCombinations(string digits) { 4 | if (digits.empty()) return _vResults; 5 | _pStrDigits = &digits; 6 | string strCombination; 7 | Backtrack(0, strCombination); 8 | return _vResults; 9 | } 10 | 11 | void Backtrack(int index, string& combination) { 12 | if (index == _pStrDigits->size()) 13 | _vResults.push_back(combination); 14 | else { 15 | for (const char& c : _mapPhone.at((*_pStrDigits)[index])) { 16 | combination.push_back(c); 17 | Backtrack(index + 1, combination); 18 | combination.pop_back(); 19 | } 20 | } 21 | } 22 | 23 | private: 24 | vector _vResults; 25 | string* _pStrDigits; 26 | unordered_map _mapPhone{ 27 | {'2', "abc"}, {'3', "def"}, 28 | {'4', "ghi"}, {'5', "jkl"}, 29 | {'6', "mno"}, {'7', "pqrs"}, 30 | {'8', "tuv"}, {'9', "wxyz"} 31 | }; 32 | }; -------------------------------------------------------------------------------- /59. spiral-matrix-ii/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generateMatrix(int n) { 4 | vector> vResult(n, vector(n)); 5 | int iNumCount = pow(n, 2), iIndex = 1; 6 | int iLimitUp = 0, iLimitDown = n - 1, iLimitLeft = 0, iLimitRight = n - 1; 7 | while (true) { 8 | for (int i = iLimitLeft; i <= iLimitRight && iIndex <= iNumCount; i++) vResult[iLimitUp][i] = iIndex, iIndex++; 9 | if (++iLimitUp > iLimitDown) break; 10 | for (int i = iLimitUp; i <= iLimitDown && iIndex <= iNumCount; i++) vResult[i][iLimitRight] = iIndex, iIndex++; 11 | if (--iLimitRight < iLimitLeft) break; 12 | for (int i = iLimitRight; i >= iLimitLeft && iIndex <= iNumCount; i--) vResult[iLimitDown][i] = iIndex, iIndex++; 13 | if (--iLimitDown < iLimitUp) break; 14 | for (int i = iLimitDown; i >= iLimitUp && iIndex <= iNumCount; i--) vResult[i][iLimitLeft] = iIndex, iIndex++; 15 | if (++iLimitLeft > iLimitRight) break; 16 | } 17 | return vResult; 18 | } 19 | }; -------------------------------------------------------------------------------- /304. range-sum-query-2d-immutable/main.cpp: -------------------------------------------------------------------------------- 1 | class NumMatrix { 2 | public: 3 | NumMatrix(vector>& matrix) { 4 | int iRowCount = matrix.size(); 5 | if (iRowCount > 0) { 6 | int iColumnCount = matrix[0].size(); 7 | _vvSumMatrix.resize(iRowCount + 1, vector(iColumnCount + 1)); 8 | for (int i = 0; i < iRowCount; i++) { 9 | for (int j = 0; j < iColumnCount; j++) { 10 | _vvSumMatrix[i + 1][j + 1] = _vvSumMatrix[i + 1][j] + _vvSumMatrix[i][j + 1] - _vvSumMatrix[i][j] + matrix[i][j]; 11 | } 12 | } 13 | } 14 | } 15 | 16 | int sumRegion(int row1, int col1, int row2, int col2) { 17 | return _vvSumMatrix[row2 + 1][col2 + 1] - _vvSumMatrix[row1][col2 + 1] - _vvSumMatrix[row2 + 1][col1] + _vvSumMatrix[row1][col1]; 18 | } 19 | private: 20 | vector> _vvSumMatrix; 21 | }; 22 | 23 | /** 24 | * Your NumMatrix object will be instantiated and called as such: 25 | * NumMatrix* obj = new NumMatrix(matrix); 26 | * int param_1 = obj->sumRegion(row1,col1,row2,col2); 27 | */ -------------------------------------------------------------------------------- /2. add-two-numbers/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 14 | ListNode* pHead = nullptr, *pTail = nullptr; 15 | int iCarry = 0; 16 | while (l1 || l2) { 17 | int iNum_1 = l1 ? l1->val : 0, iNum_2 = l2 ? l2->val : 0; 18 | int iSum = iNum_1 + iNum_2 + iCarry; 19 | if (!pHead) { 20 | pHead = pTail = new ListNode(iSum % 10); 21 | } else { 22 | pTail->next = new ListNode(iSum % 10); 23 | pTail = pTail->next; 24 | } 25 | iCarry = iSum / 10; 26 | l1 = l1 ? l1->next : l1; 27 | l2 = l2 ? l2->next : l2; 28 | } 29 | if (iCarry) pTail->next = new ListNode(iCarry); 30 | return pHead; 31 | } 32 | }; -------------------------------------------------------------------------------- /23. merge-k-sorted-lists/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* mergeKLists(vector& lists) { 14 | if (!lists.size()) return nullptr; 15 | vector vPNodes; 16 | for (ListNode* list : lists) { 17 | if (!list) continue; 18 | while(list) { 19 | vPNodes.push_back(list); 20 | list = list->next; 21 | } 22 | } 23 | if (!vPNodes.size()) return nullptr; 24 | sort(vPNodes.begin(), vPNodes.end(), [&](ListNode* pNode1, ListNode* pNode2) {return pNode1->val < pNode2->val;}); 25 | int iLength = vPNodes.size(); 26 | for (int i = 0; i < iLength - 1; i++) 27 | vPNodes[i]->next = vPNodes[i + 1]; 28 | vPNodes[iLength - 1]->next = nullptr; 29 | return vPNodes[0]; 30 | } 31 | }; -------------------------------------------------------------------------------- /173. binary-search-tree-iterator/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class BSTIterator { 13 | public: 14 | BSTIterator(TreeNode* root) { 15 | inorder(root, vNodes); 16 | } 17 | 18 | int next() { 19 | return vNodes[iIndex++]; 20 | } 21 | 22 | bool hasNext() { 23 | return iIndex < vNodes.size(); 24 | } 25 | 26 | private: 27 | vector vNodes; 28 | int iIndex = 0; 29 | void inorder(TreeNode* root, vector& vNodes) { 30 | if (!root) return; 31 | inorder(root->left, vNodes); 32 | vNodes.push_back(root->val); 33 | inorder(root->right, vNodes); 34 | } 35 | }; 36 | 37 | /** 38 | * Your BSTIterator object will be instantiated and called as such: 39 | * BSTIterator* obj = new BSTIterator(root); 40 | * int param_1 = obj->next(); 41 | * bool param_2 = obj->hasNext(); 42 | */ -------------------------------------------------------------------------------- /1143. longest-common-subsequence/README.md: -------------------------------------------------------------------------------- 1 | # 1143. longest-common-subsequence 2 | 3 | [原题链接](https://leetcode-cn.com/problems/longest-common-subsequence/) 4 | 5 | 好家伙,果然又是一道动态规划的题! 6 | 7 | 刚开始压根就没想到用动态规划,做着做着题题干也忘记了最关键的一个细节: 8 | 要求求的是**最长**公共子序列,然而我废了老鼻子劲写了个第一个最长子序列的代码: 9 | 10 | ```c++ 11 | class Solution { 12 | public: 13 | int longestCommonSubsequence(string text1, string text2) { 14 | string strShorter, strLonger; 15 | text1.size() < text2.size() 16 | ? (strShorter = text1, strLonger = text2) 17 | : (strShorter = text2, strLonger = text1); 18 | int iIndexStart = 0, iResult = 0; 19 | for (int iIndexShorter = 0; iIndexShorter < strShorter.size(); iIndexShorter++) 20 | for (int iIndexLonger = iIndexStart; iIndexLonger < strLonger.size(); iIndexLonger++) 21 | if (strShorter[iIndexShorter] == strLonger[iIndexLonger]) { 22 | cout << strShorter[iIndexShorter] << " " << strLonger[iIndexLonger] << endl; 23 | iResult++, iIndexStart = ++iIndexLonger; 24 | break; 25 | } 26 | return iResult; 27 | } 28 | }; 29 | ``` 30 | 31 | 去啃动态规划啦~ 32 | 33 | 完了……看不懂了……等专门学一下动态规划再回来啃吧 34 | 第一次失败的这么彻底,淦! 35 | 36 | > 2021 - 04 - 03 37 | -------------------------------------------------------------------------------- /224. basic-calculator/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calculate(string s) { 4 | stack stkSigns; 5 | int iResult = 0, iSign = 1, iLength = s.size(), iIndex = 0; 6 | stkSigns.push(iSign); 7 | while (iIndex < iLength) { 8 | switch(s[iIndex]) { 9 | case ' ': 10 | iIndex++; 11 | break; 12 | case '+': 13 | iSign = stkSigns.top(); 14 | iIndex++; 15 | break; 16 | case '-': 17 | iSign = -stkSigns.top(); 18 | iIndex++; 19 | break; 20 | case '(': 21 | stkSigns.push(iSign); 22 | iIndex++; 23 | break; 24 | case ')': 25 | stkSigns.pop(); 26 | iIndex++; 27 | break; 28 | default: 29 | long _iSumTmp = 0; 30 | while (iIndex < iLength && s[iIndex] >= '0' && s[iIndex] <= '9') 31 | _iSumTmp = _iSumTmp * 10 + s[iIndex] - '0', iIndex++; 32 | iResult += iSign * _iSumTmp; 33 | } 34 | } 35 | return iResult; 36 | } 37 | }; -------------------------------------------------------------------------------- /15. 3sum/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | int iLengthNums = nums.size(); 5 | if (iLengthNums < 3) return {}; 6 | vector> vResults; 7 | sort(nums.begin(), nums.end()); 8 | for (int i = 0; i < iLengthNums; i++) { 9 | if (nums[i] > 0) return vResults; 10 | if (i > 0 && nums[i] == nums[i - 1]) continue; 11 | int _iIndexLeft = i + 1, _iIndexRight = iLengthNums - 1; 12 | while (_iIndexLeft < _iIndexRight) { 13 | int _iTwoSum = nums[_iIndexLeft] + nums[_iIndexRight] ; 14 | if (_iTwoSum> -nums[i]) 15 | _iIndexRight--; 16 | else if (_iTwoSum < -nums[i]) 17 | _iIndexLeft++; 18 | else { 19 | vResults.push_back(vector({nums[i], nums[_iIndexLeft], nums[_iIndexRight]})); 20 | _iIndexLeft++, _iIndexRight--; 21 | while (_iIndexLeft < _iIndexRight && nums[_iIndexLeft] == nums[_iIndexLeft - 1]) _iIndexLeft++; 22 | while (_iIndexLeft < _iIndexRight && nums[_iIndexRight] == nums[_iIndexRight + 1]) _iIndexRight--; 23 | } 24 | } 25 | } 26 | return vResults; 27 | } 28 | }; -------------------------------------------------------------------------------- /53. maximum-subarray/README.md: -------------------------------------------------------------------------------- 1 | # 53. maximum-subarray 2 | 3 | [原题链接](https://leetcode-cn.com/problems/maximum-subarray/) 4 | 5 | 我竟然没看到 **连续** 子数组,写了一版没用的代码: 6 | 7 | ```c++ 8 | class Solution { 9 | public: 10 | int maxSubArray(vector& nums) { 11 | int iResult = 0; 12 | sort(nums.begin(), nums.end()); 13 | if (nums[nums.size() - 1] <= 0) return nums[nums.size() - 1]; 14 | for (int num : nums) 15 | if (num > 0) iResult += num; else break; 16 | return iResult; 17 | } 18 | }; 19 | ``` 20 | 21 | 懂了,这就去暴力! 22 | 23 | 好家伙这个不能用暴力! 24 | 不过发现了一版 [神仙代码](https://leetcode-cn.com/problems/maximum-subarray/solution/hua-jie-suan-fa-53-zui-da-zi-xu-he-by-guanpengchn/),这就去抄! 25 | 26 | 淦!忍不住试了一下暴力,果然超出时间限制了…… 27 | 28 | ```c++ 29 | class Solution { 30 | public: 31 | int maxSubArray(vector& nums) { 32 | int iLength = nums.size(), iResult = nums[0]; 33 | for (int length = 1; length <= iLength; length++) { 34 | for (int i = 0; i <= iLength - length; i++) { 35 | int iSum = 0; 36 | for (int j = i; j < i + length; j++) 37 | iSum += nums[j]; 38 | iResult = max(iResult, iSum); 39 | } 40 | } 41 | return iResult; 42 | } 43 | }; 44 | ``` 45 | 46 | 等会儿……这玩意是简单难度?我才意识到…… 47 | 48 | > 2021 - 03 - 26 49 | -------------------------------------------------------------------------------- /706. design-hashmap/main.cpp: -------------------------------------------------------------------------------- 1 | struct Element {int key, value;}; 2 | 3 | class MyHashMap { 4 | public: 5 | /** Initialize your data structure here. */ 6 | MyHashMap() { 7 | 8 | } 9 | 10 | /** value will always be non-negative. */ 11 | void put(int key, int value) { 12 | for (Element& element : _vElements) 13 | if (element.key == key) 14 | { 15 | element.value = value; 16 | return; 17 | } 18 | Element element = {key, value}; 19 | _vElements.push_back(element); 20 | } 21 | 22 | /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ 23 | int get(int key) { 24 | for (const Element element : _vElements) 25 | if (element.key == key) 26 | return element.value; 27 | return -1; 28 | } 29 | 30 | /** Removes the mapping of the specified value key if this map contains a mapping for the key */ 31 | void remove(int key) { 32 | _vElements.erase(remove_if(_vElements.begin(), _vElements.end(), [&](Element element){ return element.key == key; }), _vElements.end()); 33 | } 34 | private: 35 | vector _vElements; 36 | }; 37 | 38 | /** 39 | * Your MyHashMap object will be instantiated and called as such: 40 | * MyHashMap* obj = new MyHashMap(); 41 | * obj->put(key,value); 42 | * int param_2 = obj->get(key); 43 | * obj->remove(key); 44 | */ -------------------------------------------------------------------------------- /456. 132-pattern/README.md: -------------------------------------------------------------------------------- 1 | # 456. 132-pattern 2 | 3 | [原题链接](https://leetcode-cn.com/problems/132-pattern/) 4 | 5 | 第一反应是 —— 这是 `中等` 难度的题?这也太简单了吧! 6 | 于是随手写出了下面的代码: 7 | 8 | ```c++ 9 | class Solution { 10 | public: 11 | bool find132pattern(vector& nums) { 12 | int iIndex = 0, iLength = nums.size(); 13 | while (iIndex + 2 < iLength) { 14 | if (nums[iIndex] < nums[iIndex + 2] && nums[iIndex + 2] < nums[iIndex + 1]) 15 | return true; 16 | iIndex++; 17 | } 18 | return false; 19 | } 20 | }; 21 | ``` 22 | 23 | 控制台测试用例通过后我更沾沾自喜了,结果在提交后发现事情没有这么简单 24 | `i, j, k` 三个下标可能并不是连续的 25 | 正当我脑袋里面除了暴力没有其他解题思路准备看题解的时候 26 | 突然想到了一个优化后的 “暴力解法” 27 | 28 | ```c++ 29 | class Solution { 30 | public: 31 | bool find132pattern(vector& nums) { 32 | int i = 0, iLength = nums.size(); 33 | while (i + 2 < iLength) { 34 | int iIndexK = i + 1; 35 | for (int k = iLength - 1; k > i; k--) { 36 | if (nums[k] <= nums[i]) continue; 37 | for (int j = i + 1; j < k; j++) 38 | if (nums[k] < nums[j]) return true; 39 | } 40 | i++; 41 | } 42 | return false; 43 | } 44 | }; 45 | ``` 46 | 47 | 写完也改完了才意识到似乎并没有优化多少 48 | 一提交,果然超出时间限制了…… 49 | 中等难度确实有挑战性,去看下大佬们的题解吧 50 | 有种预感这题目似乎是一种类型,学会后应该不难做 51 | 52 | **单调栈** WDNMD,看了一会走神,又回来看,如此往复一上午没了 53 | 这玩意不是人做的……(只看代码是真滴难理解,确实得配合题解讲述) 54 | 那个在单调栈里面用二分法的解法我寻思应该不是现在的我能掌握的东西 55 | `我合不上.jpg` 56 | 57 | > 2021 - 03 - 24 58 | -------------------------------------------------------------------------------- /208. implement-trie-prefix-tree/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Trie { 2 | public: 3 | /** Initialize your data structure here. */ 4 | Trie() : children(26), isEnd(false) { 5 | 6 | } 7 | 8 | /** Inserts a word into the trie. */ 9 | void insert(string word) { 10 | Trie* node = this; 11 | for (char ch : word) { 12 | ch -= 'a'; 13 | if (!node->children[ch]) 14 | node->children[ch] = new Trie(); 15 | node = node->children[ch]; 16 | } 17 | node->isEnd = true; 18 | } 19 | 20 | /** Returns if the word is in the trie. */ 21 | bool search(string word) { 22 | Trie* node = this->searchPrefix(word); 23 | return node && node->isEnd; 24 | } 25 | 26 | /** Returns if there is any word in the trie that starts with the given prefix. */ 27 | bool startsWith(string prefix) { 28 | return this->searchPrefix(prefix); 29 | } 30 | 31 | private: 32 | vector children; 33 | bool isEnd; 34 | Trie* searchPrefix(string prefix) { 35 | Trie* node = this; 36 | for (char ch : prefix) { 37 | ch -= 'a'; 38 | if (!node->children[ch]) return nullptr; 39 | node = node->children[ch]; 40 | } 41 | return node; 42 | } 43 | }; 44 | 45 | /** 46 | * Your Trie object will be instantiated and called as such: 47 | * Trie* obj = new Trie(); 48 | * obj->insert(word); 49 | * bool param_2 = obj->search(word); 50 | * bool param_3 = obj->startsWith(prefix); 51 | */ -------------------------------------------------------------------------------- /2. add-two-numbers/README.md: -------------------------------------------------------------------------------- 1 | # 2. add-two-numbers 2 | 3 | [原题链接](https://leetcode-cn.com/problems/add-two-numbers/) 4 | 5 | 没有注意到节点数在 `[1, 100]` 这个条件吃了大亏 6 | 也就是说如果先使用整数或浮点数存储输入链表的结果 7 | 那么在临界测试时必然会出现溢出的情况 8 | 也就是下面这部分代码 9 | 10 | ```C++ 11 | /** 12 | * Definition for singly-linked list. 13 | * struct ListNode { 14 | * int val; 15 | * ListNode *next; 16 | * ListNode() : val(0), next(nullptr) {} 17 | * ListNode(int x) : val(x), next(nullptr) {} 18 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 19 | * }; 20 | */ 21 | class Solution { 22 | public: 23 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 24 | int iValue_1 = 0, iValue_2 = 0, iTimes = 0, iResult = 0; 25 | while (l1) { 26 | iValue_1 += l1->val * pow(10, iTimes); 27 | l1 = l1->next; 28 | iTimes++; 29 | } 30 | iTimes = 0; 31 | while (l2) { 32 | iValue_2 += l2->val * pow(10, iTimes); 33 | l2 = l2->next; 34 | iTimes++; 35 | } 36 | iResult = iValue_1 + iValue_2; 37 | ListNode* pResult = new ListNode(iResult % 10), *pTail = pResult; 38 | if (iResult < 10) return pResult; 39 | iResult = iResult / 10; 40 | while (iResult) { 41 | ListNode* pNewNode = new ListNode(iResult % 10); 42 | pTail->next = pNewNode; 43 | pTail = pNewNode; 44 | iResult = iResult / 10; 45 | } 46 | return pResult; 47 | } 48 | }; 49 | ``` 50 | 51 | 没办法,只好换一种解题思路 52 | 根据官方题解,可以使用逐位相加的方法来避免溢出的情况 53 | (突然想到,拿Python做或许就不会出现这种情况了) 54 | 55 | > 2021 - 03 - 02 -------------------------------------------------------------------------------- /6. zigzag-conversion/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convert(string s, int numRows) { 4 | vector vStrLineList(numRows); 5 | int iIndexLine = 0; 6 | bool bIsDown = true; 7 | switch(numRows) { 8 | case 1: 9 | return s; 10 | case 2: 11 | for(int i = 0; i < s.size(); i++) { 12 | vStrLineList[iIndexLine] += s[i]; 13 | iIndexLine = ++iIndexLine % 2; 14 | } 15 | return vStrLineList[0].append(vStrLineList[1]); 16 | default: 17 | for(int i = 0; i < s.size(); i++) { 18 | if (bIsDown) { 19 | vStrLineList[iIndexLine] += s[i]; 20 | if (iIndexLine + 1 > numRows - 1) { 21 | bIsDown = !bIsDown; 22 | iIndexLine--; 23 | } else { 24 | iIndexLine++; 25 | } 26 | } else { 27 | vStrLineList[iIndexLine] += s[i]; 28 | if (iIndexLine - 1 < 1) { 29 | bIsDown = !bIsDown; 30 | iIndexLine = 0; 31 | } else { 32 | iIndexLine--; 33 | } 34 | } 35 | } 36 | for (int i = 1; i < numRows; i++) 37 | vStrLineList[0].append(vStrLineList[i]); 38 | return vStrLineList[0]; 39 | } 40 | } 41 | }; -------------------------------------------------------------------------------- /5. longest-palindromic-substring/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestPalindrome(string s) { 4 | vector vPailStrList; 5 | int iStrLength = s.size(); 6 | /* 检索奇数长度的回文串 */ 7 | for(int iExpandCenter = 0; iExpandCenter < iStrLength; iExpandCenter++) { 8 | int _iExpandLength = 1; 9 | while(iExpandCenter - _iExpandLength >= 0 10 | && iExpandCenter + _iExpandLength < iStrLength 11 | && s[iExpandCenter - _iExpandLength] == s[iExpandCenter + _iExpandLength]) 12 | _iExpandLength++; 13 | vPailStrList.push_back(s.substr(iExpandCenter - (_iExpandLength - 1), (_iExpandLength - 1) * 2 + 1)); 14 | } 15 | /* 检索偶数长度的回文串 */ 16 | for(int iExpandCenterLeft = 0; iExpandCenterLeft + 1 < iStrLength; iExpandCenterLeft++) { 17 | if (s[iExpandCenterLeft] == s[iExpandCenterLeft + 1]) { 18 | int _iExpandLength = 1; 19 | while(iExpandCenterLeft - _iExpandLength >= 0 20 | && iExpandCenterLeft + _iExpandLength + 1 < iStrLength 21 | && s[iExpandCenterLeft - _iExpandLength] == s[iExpandCenterLeft + _iExpandLength + 1]) 22 | _iExpandLength++; 23 | vPailStrList.push_back(s.substr(iExpandCenterLeft - (_iExpandLength - 1), (_iExpandLength - 1) * 2 + 2)); 24 | } 25 | } 26 | 27 | /* 查找并返回最长回文串 */ 28 | int iMaxStrLength = 0; 29 | string strMax; 30 | for (auto str : vPailStrList) 31 | if (str.size() > iMaxStrLength){ 32 | strMax = str; 33 | iMaxStrLength = str.size(); 34 | } 35 | return strMax; 36 | } 37 | }; -------------------------------------------------------------------------------- /341. flatten-nested-list-iterator/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * // This is the interface that allows for creating nested lists. 3 | * // You should not implement it, or speculate about its implementation 4 | * class NestedInteger { 5 | * public: 6 | * // Return true if this NestedInteger holds a single integer, rather than a nested list. 7 | * bool isInteger() const; 8 | * 9 | * // Return the single integer that this NestedInteger holds, if it holds a single integer 10 | * // The result is undefined if this NestedInteger holds a nested list 11 | * int getInteger() const; 12 | * 13 | * // Return the nested list that this NestedInteger holds, if it holds a nested list 14 | * // The result is undefined if this NestedInteger holds a single integer 15 | * const vector &getList() const; 16 | * }; 17 | */ 18 | 19 | class NestedIterator { 20 | public: 21 | NestedIterator(vector &nestedList) { 22 | for (NestedInteger object : nestedList) 23 | _PushValue(object); 24 | _iLength = _vValues.size(); 25 | } 26 | 27 | int next() { 28 | return _vValues[_iIndex++]; 29 | } 30 | 31 | bool hasNext() { 32 | return _iIndex < _iLength; 33 | } 34 | private: 35 | vector _vValues; 36 | int _iIndex = 0, _iLength = 0; 37 | void _PushValue(const NestedInteger& object) { 38 | if (object.isInteger()) 39 | _vValues.push_back(object.getInteger()); 40 | else 41 | for (NestedInteger _object : object.getList()) 42 | _PushValue(_object); 43 | } 44 | }; 45 | 46 | /** 47 | * Your NestedIterator object will be instantiated and called as such: 48 | * NestedIterator i(nestedList); 49 | * while (i.hasNext()) cout << i.next(); 50 | */ -------------------------------------------------------------------------------- /18. 4sum/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> fourSum(vector& nums, int target) { 4 | vector> vvResults; 5 | if (nums.size() < 4) return vvResults; 6 | sort(nums.begin(), nums.end()); 7 | int iLength = nums.size(); 8 | for(int i = 0; i < iLength - 3; i++) { 9 | if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break; 10 | if (i > 0 && nums[i] == nums[i - 1]) continue; 11 | if (nums[i] + nums[iLength - 1] + nums[iLength - 2] + nums[iLength - 3] < target) continue; 12 | for (int j = i + 1; j < iLength - 2; j++) { 13 | if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break; 14 | if (j > i + 1 && nums[j] == nums[j - 1]) continue; 15 | if (nums[i] + nums[j] + nums[iLength - 1] + nums[iLength - 2] < target) continue; 16 | int iLeftIndex = j + 1, iRightIndex = iLength - 1; 17 | while (iLeftIndex < iRightIndex) { 18 | int _iSum = nums[i] + nums[j] + nums[iLeftIndex] + nums[iRightIndex]; 19 | if (_iSum == target) { 20 | vvResults.push_back({nums[i], nums[j], nums[iLeftIndex], nums[iRightIndex]}); 21 | while (iLeftIndex < iRightIndex && nums[iLeftIndex] == nums[iLeftIndex + 1]) iLeftIndex++; 22 | iLeftIndex++; 23 | while (iLeftIndex < iRightIndex && nums[iRightIndex] == nums[iRightIndex - 1]) iRightIndex--; 24 | iRightIndex--; 25 | } else if (_iSum < target) { 26 | iLeftIndex++; 27 | } else { 28 | iRightIndex--; 29 | } 30 | } 31 | } 32 | } 33 | return vvResults; 34 | } 35 | }; -------------------------------------------------------------------------------- /82. remove-duplicates-from-sorted-list-ii/README.md: -------------------------------------------------------------------------------- 1 | # 82. remove-duplicates-from-sorted-list-ii 2 | 3 | [原题链接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/) 4 | 5 | 淦!没仔细读题就开工了…… 6 | 还以为只是删除重复元素,原来是把重复过的元素全部删除…… 7 | (以下是只删除重复的元素代码) 8 | 9 | ```c++ 10 | /** 11 | * Definition for singly-linked list. 12 | * struct ListNode { 13 | * int val; 14 | * ListNode *next; 15 | * ListNode() : val(0), next(nullptr) {} 16 | * ListNode(int x) : val(x), next(nullptr) {} 17 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 18 | * }; 19 | */ 20 | class Solution { 21 | public: 22 | ListNode* deleteDuplicates(ListNode* head) { 23 | if (!head) return head; 24 | ListNode* pRawHead = head; 25 | while (head->next) { 26 | if (head->val == head->next->val) head->next = head->next->next; 27 | head = head->next; 28 | } 29 | return pRawHead; 30 | } 31 | }; 32 | ``` 33 | 34 | 中等难度题目果然不简单,仿照一 Python 大佬的代码写了一份 C++ 的 35 | 结果出错了……算了,搞一份 Python 的,然后老老实实拿双指针写吧 36 | 37 | ```c++ 38 | /** 39 | * Definition for singly-linked list. 40 | * struct ListNode { 41 | * int val; 42 | * ListNode *next; 43 | * ListNode() : val(0), next(nullptr) {} 44 | * ListNode(int x) : val(x), next(nullptr) {} 45 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 46 | * }; 47 | */ 48 | class Solution { 49 | public: 50 | ListNode* deleteDuplicates(ListNode* head) { 51 | return deleteDuplicates(head, head->val); 52 | } 53 | 54 | ListNode* deleteDuplicates(ListNode* head, int val) { 55 | if (!head) return head; 56 | if ((head && head->val == val) || (head->next && head->val == head->next->val)) 57 | return deleteDuplicates(head->next, head->val); 58 | head->next = deleteDuplicates(head->next, head->val); 59 | return head; 60 | } 61 | }; 62 | ``` 63 | 64 | > 2021 - 03 - 25 65 | -------------------------------------------------------------------------------- /54. spiral-matrix/README.md: -------------------------------------------------------------------------------- 1 | # 54. spiral-matrix 2 | 3 | [原题链接](https://leetcode-cn.com/problems/spiral-matrix/) 4 | 5 | 乍一看这不就是游戏中 AI 的寻路实现嘛,碰墙就拐弯 6 | 二话不说就写了个状态机,结果访问越界了(应该是这个问题) 7 | 今天写 [Lua 的 JSON](https://github.com/VoidmatrixHeathcliff/LuaLibs/tree/main/LuaJsonLib 8 | ) 库写的我头晕脑胀,也懒得 Debug 了 9 | 把出错的半成品代码搁这里留念一下吧,解题还得看大佬们的答案 10 | 11 | ```c++ 12 | #define DIRECTION_NONE 0 13 | #define DIRECTION_UP 1 14 | #define DIRECTION_DOWN 2 15 | #define DIRECTION_LEFT 3 16 | #define DIRECTION_RIGHT 4 17 | 18 | class Solution { 19 | public: 20 | vector spiralOrder(vector>& matrix) { 21 | vector vResult; 22 | int iLimitUp = 0, iLimitDown = matrix.size(), iLimitLeft = 0, iLimitRight = matrix[0].size(); 23 | int x = 0, y = 0, direction = DIRECTION_RIGHT; 24 | while (direction != DIRECTION_NONE) { 25 | switch (direction) { 26 | case DIRECTION_RIGHT: 27 | if (x < iLimitRight) 28 | vResult.push_back(matrix[y][x]), x++; 29 | else 30 | direction = DIRECTION_DOWN, iLimitRight--; 31 | break; 32 | case DIRECTION_DOWN: 33 | if (y < iLimitDown) 34 | vResult.push_back(matrix[y][x]), y++; 35 | else 36 | direction = DIRECTION_LEFT, iLimitDown--; 37 | break; 38 | case DIRECTION_LEFT: 39 | if (x > iLimitLeft) 40 | vResult.push_back(matrix[y][x]), x--; 41 | else 42 | direction = DIRECTION_UP, iLimitLeft++; 43 | break; 44 | case DIRECTION_UP: 45 | if (y < iLimitUp) 46 | vResult.push_back(matrix[y][x]), y--; 47 | else 48 | direction = DIRECTION_RIGHT, iLimitUp++; 49 | break; 50 | } 51 | if (iLimitLeft >= iLimitRight || iLimitDown <= iLimitUp) direction = DIRECTION_NONE; 52 | } 53 | return vResult; 54 | } 55 | }; 56 | ``` 57 | 58 | ``` 59 | Line 1034: Char 9: runtime error: reference binding to misaligned address 0xbebebebebebebeca for type 'int', which requires 4 byte alignment (stl_vector.h) 60 | 0xbebebebebebebeca: note: pointer points here 61 | 62 | SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9 63 | ``` 64 | 65 | > 2021 - 03 - 15 66 | -------------------------------------------------------------------------------- /304. range-sum-query-2d-immutable/README.md: -------------------------------------------------------------------------------- 1 | # 304. range-sum-query-2d-immutable 2 | 3 | [原题链接](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/) 4 | 5 | **NMD 为什么?!** 6 | 7 | 我大意了,没有闪,303题也确实没有想象中的那么简单,304让我看到了残酷的现实 8 | 刷个题还要考虑 OJ 机制,难道说初始化对象的过程不计入总时间? 9 | 用思路最简单清晰明了的算法(如下)彻底被这个该死的机制打败了…… 10 | 去学个🔨的前缀和算法 ~~(不过学还是要学的,虽然还想再骂一句)~~ 11 | 12 | ```c++ 13 | class NumMatrix { 14 | public: 15 | NumMatrix(vector>& matrix) { 16 | _vvMatrix = matrix; 17 | } 18 | 19 | int sumRegion(int row1, int col1, int row2, int col2) { 20 | int iResult = 0; 21 | for(int i = row1; i <= row2; i++) { 22 | for(int j = col1; j <= col2; j++) { 23 | iResult += _vvMatrix[i][j]; 24 | } 25 | } 26 | return iResult; 27 | } 28 | private: 29 | vector> _vvMatrix; 30 | }; 31 | 32 | /** 33 | * Your NumMatrix object will be instantiated and called as such: 34 | * NumMatrix* obj = new NumMatrix(matrix); 35 | * int param_1 = obj->sumRegion(row1,col1,row2,col2); 36 | */ 37 | ``` 38 | 39 | ~~等会……虽然但是——你这官方题解用的是什么语法? 40 | 为什么一个 vector 的 resize 成员函数第二个参数可以是 vector ? 41 | 这么说函数原型可以是:`void std::vector::resize(size_t Newsize, vector& val)` 42 | 难不成是我 VS2019 都不支持的 C++17 的新语法? 43 | 可是查了文档和各位大佬的博客都愣是没看到这种用法…… 44 | 不管了,反正算法大概懂了,明天自己实现吧,不爆肝了,晚安晚安,狗命要紧……~~ 45 | 46 | 果然不能在晚上写代码,看了昨晚写的鬼东西简直无地自容 47 | 嵌套 vector 的时候 resize 函数的第二个参数不就是应该是一个 vector ? 48 | 49 | ###### 我是智障 50 | ##### 我是智障 51 | #### 我是智障 52 | ### 我是智障 53 | 54 | 不是吧阿sir,这样的代码都超时吗(见下)? 55 | 看来只能去用官方答案那种让人头大的前缀和算法了 56 | 罪过,罪过…… 57 | 58 | ```c++ 59 | class NumMatrix { 60 | public: 61 | NumMatrix(vector>& matrix) { 62 | if (matrix.size() > 0) { 63 | _vvSumMatrix.resize(matrix.size(), vector(matrix[0].size(), 0)); 64 | for(int i = 0; i < matrix.size(); i++) 65 | { 66 | for(int j = 0; j < matrix[0].size(); j++) 67 | { 68 | for(int row = 0; row <= i; row++) 69 | { 70 | for(int column = 0; column <= j; column++) 71 | { 72 | _vvSumMatrix[i][j] += matrix[row][column]; 73 | } 74 | } 75 | } 76 | } 77 | } 78 | 79 | } 80 | 81 | int sumRegion(int row1, int col1, int row2, int col2) { 82 | return _vvSumMatrix[row2][col2] 83 | - (row1 > 0 ? _vvSumMatrix[row1 - 1][col2] : 0) 84 | - (col1 > 0 ? _vvSumMatrix[row2][col1 - 1] : 0) 85 | + (row1 > 0 && col1 > 0 ? _vvSumMatrix[row1 - 1][col1 - 1] : 0); 86 | } 87 | private: 88 | vector> _vvSumMatrix; 89 | }; 90 | 91 | /** 92 | * Your NumMatrix object will be instantiated and called as such: 93 | * NumMatrix* obj = new NumMatrix(matrix); 94 | * int param_1 = obj->sumRegion(row1,col1,row2,col2); 95 | */ 96 | ``` 97 | 98 | 附:最后,不得不说,前缀和算法是真滴妙~ 99 | 100 | > 2021 - 03 - 02 ~ 2021 - 03 - 03 101 | -------------------------------------------------------------------------------- /38. count-and-say/main_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | string aryResults[][4463] = { 5 | "", "1", 6 | "11", 7 | "21", 8 | "1211", 9 | "111221", 10 | "312211", 11 | "13112221", 12 | "1113213211", 13 | "31131211131221", 14 | "13211311123113112211", 15 | "11131221133112132113212221", 16 | "3113112221232112111312211312113211", 17 | "1321132132111213122112311311222113111221131221", 18 | "11131221131211131231121113112221121321132132211331222113112211", 19 | "311311222113111231131112132112311321322112111312211312111322212311322113212221", 20 | "132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211", 21 | "11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221", 22 | "31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211", 23 | "1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221", 24 | "11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211", 25 | "311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311122122111312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221131112311311121321122112132231121113122113322113111221131221", 26 | "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113213221133122112231131122211211131221131112311332211211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211", 27 | "111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121113222123112221221321132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221", 28 | "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211", 29 | "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132132211231232112311321322112311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312111312212231131122211311123113322112111312211312111322111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113213221132213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121132211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221", 30 | "1113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123211211131211121311121321123113111231131122112213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122113221122112133221121113122113121113222123211211131211121311121321123113213221121113122113121113222113221113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211", 31 | "31131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322123211211131211121332211231131122211311122122111312211213211312111322211231131122211311123113322112111331121113112221121113122113111231133221121113122113121113222123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221123113112221131112311332111213122112311311123112111331121113122112132113311213211321222122111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311123113322113223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331221122311311222112111312211311123113322112132113213221133122211332111213112221133211322112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121122132112311321322112111312211312111322211213111213122112132113121113222112132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212321121113121112133221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213212312311211131122211213211331121321122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311222113111221221113122112132113121113222112132113213221133122211332111213322112132113213221132231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221", 32 | "13211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221232112111312211312113211223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321322113311213212322211322132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212311322123123112111321322123122113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132132211331221122311311222112111312211311123113322112111312211312111322212311322123123112112322211211131221131211132221132213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211", 33 | "11131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221231122212213211321322112311311222113311213212322211211131221131211132221232112111312111213322112131112131221121321131211132221121321132132212321121113121112133221121321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122111213122112311311222113111221131221221321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311222112111331121113112221121113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212321121113121112133221132211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112132132112211131221131211132221121321132132212321121113121112133221123113112221131112311332111213211322111213111213211231131211132211121311222113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131211131221223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112211213322112312321123113213221123113112221133112132123222112311311222113111231132231121113112221121321133112132112211213322112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212311222122132113213221123113112221133112132123222112311311222113111231133211121321132211121311121321122112133221123113112221131112311332211322111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221", 34 | "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211" 35 | }; 36 | return aryResults[n]; 37 | } 38 | }; --------------------------------------------------------------------------------