diff --git a/problems/101.symmetric-tree.md b/problems/101.symmetric-tree.md index 5aecc4f5e..a2d0d60ad 100644 --- a/problems/101.symmetric-tree.md +++ b/problems/101.symmetric-tree.md @@ -33,6 +33,10 @@ https://leetcode-cn.com/problems/symmetric-tree/ ``` +## 前置知识 + +- 二叉树 +- 递归 ## 思路 diff --git a/problems/102.binary-tree-level-order-traversal.md b/problems/102.binary-tree-level-order-traversal.md index 747510ede..186ea48cd 100644 --- a/problems/102.binary-tree-level-order-traversal.md +++ b/problems/102.binary-tree-level-order-traversal.md @@ -25,6 +25,10 @@ https://leetcode.com/problems/binary-tree-level-order-traversal/description/ ] ``` +## 前置知识 + +- 队列 + ## 思路 这是一个典型的二叉树遍历问题, 关于二叉树遍历,我总结了一个[专题](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md),大家可以先去看下那个,然后再来刷这道题。 diff --git a/problems/103.binary-tree-zigzag-level-order-traversal.md b/problems/103.binary-tree-zigzag-level-order-traversal.md index 7cf53ec5b..70881df43 100644 --- a/problems/103.binary-tree-zigzag-level-order-traversal.md +++ b/problems/103.binary-tree-zigzag-level-order-traversal.md @@ -23,6 +23,10 @@ return its zigzag level order traversal as: ] ``` +## 前置知识 + +- 队列 + ## 思路 这道题可以借助`队列`实现,首先把root入队,然后入队一个特殊元素Null(来表示每层的结束)。 diff --git a/problems/104.maximum-depth-of-binary-tree.md b/problems/104.maximum-depth-of-binary-tree.md index a52654195..dcd859e07 100644 --- a/problems/104.maximum-depth-of-binary-tree.md +++ b/problems/104.maximum-depth-of-binary-tree.md @@ -24,6 +24,10 @@ return its depth = 3. ``` +## 前置知识 + +- 递归 + ## 思路 由于树是一种递归的数据结构,因此用递归去解决的时候往往非常容易,这道题恰巧也是如此, diff --git a/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md b/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md index 24c809165..a61eadb18 100644 --- a/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md +++ b/problems/105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md @@ -26,6 +26,10 @@ Return the following binary tree: 15 7 ``` +## 前置知识 + +- 二叉树 + ## 思路/Thinking Path 目标是构造二叉树。 diff --git a/problems/113.path-sum-ii.md b/problems/113.path-sum-ii.md index e0e3d09b1..d68d248c5 100644 --- a/problems/113.path-sum-ii.md +++ b/problems/113.path-sum-ii.md @@ -26,6 +26,10 @@ Return: ] ``` +## 前置知识 + +- 回溯法 + ## 思路 这道题目是求集合,并不是`求值`,而是枚举所有可能,因此动态规划不是特别切合,因此我们需要考虑别的方法。 diff --git a/problems/121.best-time-to-buy-and-sell-stock.md b/problems/121.best-time-to-buy-and-sell-stock.md index d30c088e3..fd779cf13 100644 --- a/problems/121.best-time-to-buy-and-sell-stock.md +++ b/problems/121.best-time-to-buy-and-sell-stock.md @@ -24,6 +24,10 @@ Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0. ``` +## 前置知识 + +- 数组 + ## 思路 由于我们是想获取到最大的利润,我们的策略应该是低点买入,高点卖出。 diff --git a/problems/122.best-time-to-buy-and-sell-stock-ii.md b/problems/122.best-time-to-buy-and-sell-stock-ii.md index 142b29928..ad9933b93 100644 --- a/problems/122.best-time-to-buy-and-sell-stock-ii.md +++ b/problems/122.best-time-to-buy-and-sell-stock-ii.md @@ -31,6 +31,10 @@ Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0. ``` +## 前置知识 + +- 数组 + ## 思路 由于我们是想获取到最大的利润,我们的策略应该是低点买入,高点卖出。 diff --git a/problems/124.binary-tree-maximum-path-sum.md b/problems/124.binary-tree-maximum-path-sum.md index 1c4c6606e..e83d72dcb 100644 --- a/problems/124.binary-tree-maximum-path-sum.md +++ b/problems/124.binary-tree-maximum-path-sum.md @@ -31,6 +31,10 @@ Input: [-10,9,20,null,null,15,7] Output: 42 ``` +## 前置知识 + +- 递归 + ## 思路 这道题目的path让我误解了,然后浪费了很多时间来解这道题 diff --git a/problems/125.valid-palindrome.md b/problems/125.valid-palindrome.md index b7578be26..00229b244 100644 --- a/problems/125.valid-palindrome.md +++ b/problems/125.valid-palindrome.md @@ -21,6 +21,11 @@ Output: false ``` +## 前置知识 + +- 回文 +- 双指针 + ## 思路 这是一道考察回文的题目,而且是最简单的形式,即判断一个字符串是否是回文。 diff --git a/problems/128.longest-consecutive-sequence.md b/problems/128.longest-consecutive-sequence.md index 65ab0e64f..c9d38219d 100644 --- a/problems/128.longest-consecutive-sequence.md +++ b/problems/128.longest-consecutive-sequence.md @@ -21,6 +21,10 @@ Submissions ``` +## 前置知识 + +- hashmap + ## 思路 这是一道最最长连续数字序列长度的题目, 官网给出的难度是`hard`. diff --git a/problems/129.sum-root-to-leaf-numbers.md b/problems/129.sum-root-to-leaf-numbers.md index 551629cde..6ddad87f1 100644 --- a/problems/129.sum-root-to-leaf-numbers.md +++ b/problems/129.sum-root-to-leaf-numbers.md @@ -41,6 +41,10 @@ Therefore, sum = 495 + 491 + 40 = 1026. ``` +## 前置知识 + +- 递归 + ## 思路 这是一道非常适合训练递归的题目。虽然题目不难,但是要想一次写正确,并且代码要足够优雅却不是很容易。 diff --git a/problems/130.surrounded-regions.md b/problems/130.surrounded-regions.md index 060ec587d..789467fa0 100644 --- a/problems/130.surrounded-regions.md +++ b/problems/130.surrounded-regions.md @@ -27,6 +27,10 @@ Surrounded regions shouldn’t be on the border, which means that any 'O' on the ``` +## 前置知识 + +- DFS + ## 思路 我们需要将所有被X包围的O变成X,并且题目明确说了边缘的所有O都是不可以变成X的。 diff --git a/problems/131.palindrome-partitioning.md b/problems/131.palindrome-partitioning.md index e241a9d40..4efaea54e 100644 --- a/problems/131.palindrome-partitioning.md +++ b/problems/131.palindrome-partitioning.md @@ -21,6 +21,10 @@ Output: ``` +## 前置知识 + +- 回溯法 + ## 思路 这是一道求解所有可能性的题目, 这时候可以考虑使用回溯法。 回溯法解题的模板我们已经在很多题目中用过了, diff --git a/problems/136.single-number.md b/problems/136.single-number.md index b3099686a..fb892ec02 100644 --- a/problems/136.single-number.md +++ b/problems/136.single-number.md @@ -12,6 +12,10 @@ Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? ``` +## 前置知识 + +- 位运算 + ## 思路 根据题目描述,由于加上了时间复杂度必须是 O(n),并且空间复杂度为 O(1)的条件,因此不能用排序方法,也不能使用 map 数据结构。 diff --git a/problems/139.word-break.md b/problems/139.word-break.md index 142b3bab5..d6c27f72a 100644 --- a/problems/139.word-break.md +++ b/problems/139.word-break.md @@ -30,6 +30,10 @@ Output: false ``` +## 前置知识 + +- 动态规划 + ## 思路 这道题是给定一个字典和一个句子,判断该句子是否可以由字典里面的单词组出来,一个单词可以用多次。 diff --git a/problems/144.binary-tree-preorder-traversal.md b/problems/144.binary-tree-preorder-traversal.md index 12e92fb1b..b283a723e 100644 --- a/problems/144.binary-tree-preorder-traversal.md +++ b/problems/144.binary-tree-preorder-traversal.md @@ -21,6 +21,11 @@ Follow up: Recursive solution is trivial, could you do it iteratively? ``` +## 前置知识 + +- 递归 +- 栈 + ## 思路 这道题目是前序遍历,这个和之前的`leetcode 94 号问题 - 中序遍历`完全不一回事。 diff --git a/problems/145.binary-tree-postorder-traversal.md b/problems/145.binary-tree-postorder-traversal.md index 0b20ea243..46e670e22 100644 --- a/problems/145.binary-tree-postorder-traversal.md +++ b/problems/145.binary-tree-postorder-traversal.md @@ -22,6 +22,11 @@ Note: Recursive solution is trivial, could you do it iteratively? ``` +## 前置知识 + +- 栈 +- 递归 + ## 思路 相比于前序遍历,后续遍历思维上难度要大些,前序遍历是通过一个stack,首先压入父亲结点,然后弹出父亲结点,并输出它的value,之后压人其右儿子,左儿子即可。 diff --git a/problems/146.lru-cache.md b/problems/146.lru-cache.md index 0abd27250..9060024b0 100644 --- a/problems/146.lru-cache.md +++ b/problems/146.lru-cache.md @@ -28,6 +28,10 @@ cache.get(4); // returns 4 ``` +## 前置知识 + +- 队列 + ## 思路 `本题已被收录到我的新书中,敬请期待~` diff --git a/problems/150.evaluate-reverse-polish-notation.md b/problems/150.evaluate-reverse-polish-notation.md index 8b56c0d5a..5cd4216dc 100644 --- a/problems/150.evaluate-reverse-polish-notation.md +++ b/problems/150.evaluate-reverse-polish-notation.md @@ -15,6 +15,11 @@ Note: Division between two integers should truncate toward zero. The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation. ``` + +## 前置知识 + +- 栈 + ## 思路 逆波兰表达式又叫做后缀表达式。在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为`中缀表示`。 diff --git a/problems/152.maximum-product-subarray.md b/problems/152.maximum-product-subarray.md index 3ed9c07eb..5767d59fb 100644 --- a/problems/152.maximum-product-subarray.md +++ b/problems/152.maximum-product-subarray.md @@ -20,6 +20,10 @@ https://leetcode.com/problems/maximum-product-subarray/description/ 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。 ``` +## 前置知识 + +- 滑动窗口 + ## 思路 这道题目要我们求解连续的 n 个数中乘积最大的积是多少。这里提到了连续,笔者首先想到的就是滑动窗口,但是这里比较特殊,我们不能仅仅维护一个最大值,因此最小值(比如-20)乘以一个比较小的数(比如-10) diff --git a/problems/155.min-stack.md b/problems/155.min-stack.md index ab07da5f8..49578ae9a 100644 --- a/problems/155.min-stack.md +++ b/problems/155.min-stack.md @@ -23,8 +23,10 @@ minStack.getMin(); --> Returns -2. ``` -# 差值法 +## 前置知识 +- 差值法 + ## 思路 符合直觉的方法是,每次对栈进行修改操作(push和pop)的时候更新最小值。 然后getMin只需要返回我们计算的最小值即可, diff --git a/problems/169.majority-element.md b/problems/169.majority-element.md index e96aa912e..9e73841be 100644 --- a/problems/169.majority-element.md +++ b/problems/169.majority-element.md @@ -20,6 +20,10 @@ Output: 2 ``` +## 前置知识 + +- 投票算法 + ## 思路 符合直觉的做法是利用额外的空间去记录每个元素出现的次数,并用一个单独的变量记录当前出现次数最多的元素。 diff --git a/problems/172.factorial-trailing-zeroes.md b/problems/172.factorial-trailing-zeroes.md index 59b25d862..034966a34 100644 --- a/problems/172.factorial-trailing-zeroes.md +++ b/problems/172.factorial-trailing-zeroes.md @@ -21,6 +21,10 @@ Note: Your solution should be in logarithmic time complexity. ``` +## 前置知识 + +- 递归 + ## 思路 我们需要求解这n个数字相乘的结果末尾有多少个0,由于题目要求log的复杂度,因此暴力求解是不行的。 diff --git a/problems/190.reverse-bits.md b/problems/190.reverse-bits.md index 768ff8c56..5472b41a4 100644 --- a/problems/190.reverse-bits.md +++ b/problems/190.reverse-bits.md @@ -27,6 +27,10 @@ In Java, the compiler represents the signed integers using 2's complement notati ``` +## 前置知识 + +- 双指针 + ## 思路 这道题是给定一个32位的无符号整型,让你按位翻转, 第一位变成最后一位, 第二位变成倒数第二位。。。 diff --git a/problems/191.number-of-1-bits.md b/problems/191.number-of-1-bits.md index b4f8bc37e..f0ab3c9fb 100644 --- a/problems/191.number-of-1-bits.md +++ b/problems/191.number-of-1-bits.md @@ -32,6 +32,10 @@ In Java, the compiler represents the signed integers using 2's complement notati ``` +## 前置知识 + +- 位运算 + ## 思路 这个题目的大意是: 给定一个无符号的整数, 返回其用二进制表式的时候的1的个数。 diff --git a/problems/198.house-robber.md b/problems/198.house-robber.md index c294a2b2b..eee4bb7f9 100644 --- a/problems/198.house-robber.md +++ b/problems/198.house-robber.md @@ -24,6 +24,10 @@ Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (m ``` +## 前置知识 + +- 动态规划 + ## 思路 这是一道非常典型且简单的动态规划问题,但是在这里我希望通过这个例子, diff --git a/problems/199.binary-tree-right-side-view.md b/problems/199.binary-tree-right-side-view.md index 40a844b26..f8f6f620b 100644 --- a/problems/199.binary-tree-right-side-view.md +++ b/problems/199.binary-tree-right-side-view.md @@ -20,6 +20,10 @@ Explanation: 5 4 <--- ``` +## 前置知识 + +- 队列 + ## 思路 > 这道题和 leetcode 102 号问题《102.binary-tree-level-order-traversal》很像 diff --git a/problems/200.number-of-islands.md b/problems/200.number-of-islands.md index 4615308d9..a44f1bfd9 100644 --- a/problems/200.number-of-islands.md +++ b/problems/200.number-of-islands.md @@ -28,6 +28,10 @@ Output: 3 ``` +## 前置知识 + +- DFS + ## 思路 如图,我们其实就是要求红色区域的个数,换句话说就是求连续区域的个数。 diff --git a/problems/201.bitwise-and-of-numbers-range.md b/problems/201.bitwise-and-of-numbers-range.md index 5d148cc8b..7957c9867 100644 --- a/problems/201.bitwise-and-of-numbers-range.md +++ b/problems/201.bitwise-and-of-numbers-range.md @@ -18,6 +18,10 @@ Output: 0 ``` +## 前置知识 + +- 位运算 + ## 思路 一个显而易见的解法是, 从m到n依次进行`求与`的操作。 diff --git a/problems/203.remove-linked-list-elements.md b/problems/203.remove-linked-list-elements.md index 7955c0052..bcfbdfb1a 100644 --- a/problems/203.remove-linked-list-elements.md +++ b/problems/203.remove-linked-list-elements.md @@ -12,6 +12,10 @@ Output: 1->2->3->4->5 ``` +## 前置知识 + +- 链表 + ## 思路 这个一个链表基本操作的题目,思路就不多说了。 ## 关键点解析 diff --git a/problems/206.reverse-linked-list.md b/problems/206.reverse-linked-list.md index 2a29e7135..c5b814340 100644 --- a/problems/206.reverse-linked-list.md +++ b/problems/206.reverse-linked-list.md @@ -12,6 +12,10 @@ Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? +## 前置知识 + +- 链表 + ## 思路 这个就是常规操作了,使用一个变量记录前驱 pre,一个变量记录后继 next. diff --git a/problems/208.implement-trie-prefix-tree.md b/problems/208.implement-trie-prefix-tree.md index 959d3e964..978d9f533 100644 --- a/problems/208.implement-trie-prefix-tree.md +++ b/problems/208.implement-trie-prefix-tree.md @@ -24,6 +24,10 @@ All inputs are guaranteed to be non-empty strings. ``` +## 前置知识 + +- 前缀树 + ## 思路 这是一道很直接的题目,上来就让你实现`前缀树(字典树)`。这算是基础数据结构中的 diff --git a/problems/209.minimum-size-subarray-sum.md b/problems/209.minimum-size-subarray-sum.md index df3c648e0..bfdb6cb6c 100644 --- a/problems/209.minimum-size-subarray-sum.md +++ b/problems/209.minimum-size-subarray-sum.md @@ -17,6 +17,10 @@ If you have figured out the O(n) solution, try coding another solution of which ``` +## 前置知识 + +- 滑动窗口 + ## 思路 用滑动窗口来记录序列, 每当滑动窗口中的 sum 超过 s, 就去更新最小值,并根据先进先出的原则更新滑动窗口,直至 sum 刚好小于 s diff --git a/problems/211.add-and-search-word-data-structure-design.md b/problems/211.add-and-search-word-data-structure-design.md index a0b4037be..63eed22f7 100644 --- a/problems/211.add-and-search-word-data-structure-design.md +++ b/problems/211.add-and-search-word-data-structure-design.md @@ -26,6 +26,10 @@ search("b..") -> true ``` +## 前置知识 + +- 前缀树 + ## 思路 我们首先不考虑字符"."的情况。这种情况比较简单,我们 addWord 直接添加到数组尾部,search 则线性查找即可。 diff --git a/problems/212.word-search-ii.md b/problems/212.word-search-ii.md index fe978fa87..ac3cafbf9 100644 --- a/problems/212.word-search-ii.md +++ b/problems/212.word-search-ii.md @@ -31,6 +31,11 @@ words = ["oath","pea","eat","rain"] and board = ``` +## 前置知识 + +- 前缀树 +- DFS + ## 思路 我们需要对矩阵中每一项都进行深度优先遍历(DFS)。 递归的终点是 diff --git a/problems/215.kth-largest-element-in-an-array.md b/problems/215.kth-largest-element-in-an-array.md index 47f741530..40c0d27bf 100644 --- a/problems/215.kth-largest-element-in-an-array.md +++ b/problems/215.kth-largest-element-in-an-array.md @@ -18,6 +18,11 @@ Note: You may assume k is always valid, 1 ≤ k ≤ array's length. ``` +## 前置知识 + +- 堆 +- Quick Select + ## 思路 这道题要求在一个无序的数组中,返回第K大的数。根据时间复杂度不同,这题有3种不同的解法。 diff --git a/problems/219.contains-duplicate-ii.md b/problems/219.contains-duplicate-ii.md index c2c85b964..4a19d9319 100644 --- a/problems/219.contains-duplicate-ii.md +++ b/problems/219.contains-duplicate-ii.md @@ -23,6 +23,10 @@ Output: false ``` +## 前置知识 + +- hashmap + ## 思路 由于题目没有对空间复杂度有求,用一个hashmap 存储已经访问过的数字即可, diff --git a/problems/221.maximal-square.md b/problems/221.maximal-square.md index 7053d7b9c..22d1150c7 100644 --- a/problems/221.maximal-square.md +++ b/problems/221.maximal-square.md @@ -21,6 +21,11 @@ Input: Output: 4 ``` +## 前置知识 + +- 动态规划 +- 递归 + ## 思路 ![221.maximal-square](../assets/problems/221.maximal-square-1.jpg) diff --git a/problems/226.invert-binary-tree.md b/problems/226.invert-binary-tree.md index 664bcf1f4..23e1fb384 100644 --- a/problems/226.invert-binary-tree.md +++ b/problems/226.invert-binary-tree.md @@ -29,6 +29,10 @@ This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off. ``` +## 前置知识 + +- 递归 + ## 思路 遍历树(随便怎么遍历),然后将左右子树交换位置。 ## 关键点解析 diff --git a/problems/229.majority-element-ii.md b/problems/229.majority-element-ii.md index bbe4b64d8..b38953280 100644 --- a/problems/229.majority-element-ii.md +++ b/problems/229.majority-element-ii.md @@ -19,6 +19,10 @@ Input: [1,1,1,3,3,2,2,2] Output: [1,2] ``` +## 前置知识 + +- 摩尔投票法 + ## 思路 这道题目和[169.majority-element](./169.majority-element.md) 很像。 diff --git a/problems/230.kth-smallest-element-in-a-bst.md b/problems/230.kth-smallest-element-in-a-bst.md index b11e07d78..c061d97e9 100644 --- a/problems/230.kth-smallest-element-in-a-bst.md +++ b/problems/230.kth-smallest-element-in-a-bst.md @@ -35,6 +35,10 @@ What if the BST is modified (insert/delete operations) often and you need to fin ``` +## 前置知识 + +- 中序遍历 + ## 思路 解法一: diff --git a/problems/232.implement-queue-using-stacks.md b/problems/232.implement-queue-using-stacks.md index d5f4b1eaf..cb311ee47 100644 --- a/problems/232.implement-queue-using-stacks.md +++ b/problems/232.implement-queue-using-stacks.md @@ -27,6 +27,10 @@ Depending on your language, stack may not be supported natively. You may simulat You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue). ``` +## 前置知识 + +- 栈 + ## 思路 这道题目是让我们用栈来模拟实现队列。 我们知道栈和队列都是一种受限的数据结构。 diff --git a/problems/236.lowest-common-ancestor-of-a-binary-tree.md b/problems/236.lowest-common-ancestor-of-a-binary-tree.md index f5a85d6e0..255bcff07 100644 --- a/problems/236.lowest-common-ancestor-of-a-binary-tree.md +++ b/problems/236.lowest-common-ancestor-of-a-binary-tree.md @@ -33,6 +33,10 @@ All of the nodes' values will be unique. p and q are different and both values will exist in the binary tree. ``` +## 前置知识 + +- 递归 + ## 思路 这道题目是求解二叉树中,两个给定节点的最近的公共祖先。是一道非常经典的二叉树题目。 diff --git a/problems/238.product-of-array-except-self.md b/problems/238.product-of-array-except-self.md index ab8a79fbc..af3c5cb5d 100644 --- a/problems/238.product-of-array-except-self.md +++ b/problems/238.product-of-array-except-self.md @@ -25,6 +25,10 @@ https://leetcode.com/problems/product-of-array-except-self/description/ ``` +## 前置知识 + +- 数组 + ## 思路 这道题的意思是给定一个数组,返回一个新的数组,这个数组每一项都是其他项的乘积。 diff --git a/problems/239.sliding-window-maximum.md b/problems/239.sliding-window-maximum.md index f45f39c07..5fc84c563 100644 --- a/problems/239.sliding-window-maximum.md +++ b/problems/239.sliding-window-maximum.md @@ -28,6 +28,11 @@ Follow up: Could you solve it in linear time? ``` +## 前置知识 + +- 队列 +- 滑动窗口 + ## 思路 符合直觉的想法是直接遍历 nums, 然后然后用一个变量 slideWindow 去承载 k 个元素, diff --git a/problems/240.search-a-2-d-matrix-ii.md b/problems/240.search-a-2-d-matrix-ii.md index 8be96b8ef..5bafd7c81 100644 --- a/problems/240.search-a-2-d-matrix-ii.md +++ b/problems/240.search-a-2-d-matrix-ii.md @@ -26,6 +26,10 @@ Given target = 20, return false. ``` +## 前置知识 + +- 数组 + ## 思路 符合直觉的做法是两层循环遍历,时间复杂度是O(m * n), diff --git a/problems/263.ugly-number.md b/problems/263.ugly-number.md index d8e04d5b9..5fa08f79e 100644 --- a/problems/263.ugly-number.md +++ b/problems/263.ugly-number.md @@ -31,6 +31,10 @@ Input is within the 32-bit signed integer range: [−231, 231 − 1]. ``` +## 前置知识 + +- 因数分解 + ## 思路 题目要求给定一个数字,判断是否为“丑陋数”(ugly number), 丑陋数是指只包含质因子2, 3, 5的正整数。 diff --git a/problems/279.perfect-squares.md b/problems/279.perfect-squares.md index 174e364a8..82da8fcaf 100644 --- a/problems/279.perfect-squares.md +++ b/problems/279.perfect-squares.md @@ -20,6 +20,11 @@ Explanation: 13 = 4 + 9. ``` +## 前置知识 + +- 递归 +- 动态规划 + ## 思路 直接递归处理即可,但是这种暴力的解法很容易超时。如果你把递归的过程化成一棵树的话(其实就是递归树), diff --git a/problems/283.move-zeroes.md b/problems/283.move-zeroes.md index f4eb15e1f..f07a18982 100644 --- a/problems/283.move-zeroes.md +++ b/problems/283.move-zeroes.md @@ -16,6 +16,11 @@ You must do this in-place without making a copy of the array. Minimize the total number of operations. ``` + +## 前置知识 + +- 数组 + ## 思路 如果题目没有要求 modify in-place 的话,我们可以先遍历一遍将包含 0 的和不包含 0 的存到两个数组, diff --git a/problems/295.find-median-from-data-stream.md b/problems/295.find-median-from-data-stream.md index 2ac1b36f6..4922b07ed 100644 --- a/problems/295.find-median-from-data-stream.md +++ b/problems/295.find-median-from-data-stream.md @@ -34,6 +34,11 @@ If 99% of all integer numbers from the stream are between 0 and 100, how would y ``` +## 前置知识 + +- 堆 +- 队列 + ## 思路 这道题目是求动态数据的中位数,在 leetcode 难度为`hard`. 如果这道题是求静态数据的中位数,我们用数组去存储, diff --git a/problems/301.remove-invalid-parentheses.md b/problems/301.remove-invalid-parentheses.md index 9e308507e..5bb9b653c 100644 --- a/problems/301.remove-invalid-parentheses.md +++ b/problems/301.remove-invalid-parentheses.md @@ -23,6 +23,11 @@ Output: [""] ``` +## 前置知识 + +- BFS +- 队列 + ## 思路 我们的思路是先写一个函数用来判断给定字符串是否是有效的。 然后再写一个函数,这个函数 diff --git a/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md b/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md index c2372d3f5..4b6f63ba6 100644 --- a/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md +++ b/problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md @@ -18,6 +18,10 @@ Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell] ``` +## 前置知识 + +- 动态规划 + ## 思路 这是一道典型的 DP 问题, DP 问题的核心是找到状态和状态转移方程。 diff --git a/problems/312.burst-balloons.md b/problems/312.burst-balloons.md index 572e7e226..b88f5f0cf 100644 --- a/problems/312.burst-balloons.md +++ b/problems/312.burst-balloons.md @@ -23,6 +23,10 @@ https://leetcode-cn.com/problems/burst-balloons/   coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 ``` +## 前置知识 + +- 回溯法 + ### 思路 #### 回溯法 diff --git a/problems/322.coin-change.md b/problems/322.coin-change.md index 3839254db..4bdc232c4 100644 --- a/problems/322.coin-change.md +++ b/problems/322.coin-change.md @@ -19,6 +19,11 @@ Note: You may assume that you have an infinite number of each kind of coin. ``` + +## 前置知识 + +- 贪心算法 + ## 思路 diff --git a/problems/328.odd-even-linked-list.md b/problems/328.odd-even-linked-list.md index d91266feb..4d50dff19 100644 --- a/problems/328.odd-even-linked-list.md +++ b/problems/328.odd-even-linked-list.md @@ -23,6 +23,10 @@ The relative order inside both the even and odd groups should remain as it was i The first node is considered odd, the second node even and so on ... ``` +## 前置知识 + +- 链表 + ## 思路 符合直觉的想法是,先遍历一遍找出奇数的节点。然后再遍历一遍找出偶数节点,最后串起来。 diff --git a/problems/334.increasing-triplet-subsequence.md b/problems/334.increasing-triplet-subsequence.md index 7c359e351..012c1ac34 100644 --- a/problems/334.increasing-triplet-subsequence.md +++ b/problems/334.increasing-triplet-subsequence.md @@ -23,6 +23,10 @@ Input: [5,4,3,2,1] Output: false ``` +## 前置知识 + +- 双指针 + ## 思路 这道题是求解顺序数字是否有三个递增的排列, 注意这里没有要求连续的,因此诸如滑动窗口的思路是不可以的。 题目要求O(n)的时间复杂度和O(1)的空间复杂度,因此暴力的做法就不用考虑了。 diff --git a/problems/335.self-crossing.md b/problems/335.self-crossing.md index 0ff94829f..2d01268ed 100644 --- a/problems/335.self-crossing.md +++ b/problems/335.self-crossing.md @@ -41,6 +41,10 @@ https://leetcode-cn.com/problems/self-crossing/ ``` +## 前置知识 + +- 滑动窗口 + ## 思路 符合直觉的做法是$O(N)$时间和空间复杂度的算法。这种算法非常简单,但是题目要求我们使用空间复杂度为$O(1)$的做法。 diff --git a/problems/342.power-of-four.md b/problems/342.power-of-four.md index b07b83f8d..13f636c08 100644 --- a/problems/342.power-of-four.md +++ b/problems/342.power-of-four.md @@ -19,6 +19,10 @@ Follow up: Could you solve it without loops/recursion? ``` +## 前置知识 + +- 数论 + ## 思路 符合直觉的做法是不停除以 4 直到不能整除,然后判断是否为 1 即可。 代码如下: diff --git a/problems/343.integer-break.md b/problems/343.integer-break.md index 6f4e73582..324e7c212 100644 --- a/problems/343.integer-break.md +++ b/problems/343.integer-break.md @@ -18,6 +18,11 @@ https://leetcode-cn.com/problems/integer-break/ 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。 说明: 你可以假设  n  不小于 2 且不大于 58。 +## 前置知识 + +- 递归 +- 动态规划 + ## 思路 希望通过这篇题解让大家知道“题解区的水有多深”,让大家知道“什么才是好的题解”。 diff --git a/problems/349.intersection-of-two-arrays.md b/problems/349.intersection-of-two-arrays.md index 17b255e4f..ed07f6b9d 100644 --- a/problems/349.intersection-of-two-arrays.md +++ b/problems/349.intersection-of-two-arrays.md @@ -22,6 +22,10 @@ The result can be in any order. ``` +## 前置知识 + +- hashtable + ## 思路 先遍历第一个数组,将其存到hashtable中, diff --git a/problems/365.water-and-jug-problem.md b/problems/365.water-and-jug-problem.md index 6db36849e..fd8dff91a 100644 --- a/problems/365.water-and-jug-problem.md +++ b/problems/365.water-and-jug-problem.md @@ -28,6 +28,11 @@ Output: False ## BFS(超时) +## 前置知识 + +- BFS +- 最大公约数 + ### 思路 两个水壶的水我们考虑成状态,然后我们不断进行倒的操作,改变状态。那么初始状态就是(0 0) 目标状态就是 (any, z)或者 (z, any),其中any 指的是任意升水。 diff --git a/problems/371.sum-of-two-integers.md b/problems/371.sum-of-two-integers.md index a52df6a53..c6f90d08f 100644 --- a/problems/371.sum-of-two-integers.md +++ b/problems/371.sum-of-two-integers.md @@ -18,6 +18,10 @@ Output: 1 ``` +## 前置知识 + +- 位运算 + ## 思路 不能使用加减法来求加法。 我们只能朝着位元算的角度来思考了。 diff --git a/problems/378.kth-smallest-element-in-a-sorted-matrix.md b/problems/378.kth-smallest-element-in-a-sorted-matrix.md index 8054da9c1..9886dd945 100644 --- a/problems/378.kth-smallest-element-in-a-sorted-matrix.md +++ b/problems/378.kth-smallest-element-in-a-sorted-matrix.md @@ -23,6 +23,11 @@ Note: You may assume k is always valid, 1 ≤ k ≤ n2. ``` +## 前置知识 + +- 二分查找 +- 堆 + ## 思路 显然用大顶堆可以解决,时间复杂度 Klogn n 为总的数字个数, diff --git a/problems/380.insert-delete-getrandom-o1.md b/problems/380.insert-delete-getrandom-o1.md index 09bb4eaa0..03f45962b 100644 --- a/problems/380.insert-delete-getrandom-o1.md +++ b/problems/380.insert-delete-getrandom-o1.md @@ -38,6 +38,11 @@ randomSet.getRandom(); ``` +## 前置知识 + +- 数组 +- 哈希表 + ## 思路 这是一个设计题。这道题的核心就是考察基本数据结构和算法的操作以及复杂度。 diff --git a/problems/416.partition-equal-subset-sum.md b/problems/416.partition-equal-subset-sum.md index 5dab5ef7a..66093dd38 100644 --- a/problems/416.partition-equal-subset-sum.md +++ b/problems/416.partition-equal-subset-sum.md @@ -27,6 +27,11 @@ > > 解释: 数组不能分割成两个元素和相等的子集. +## 前置知识 + +- DFS +- 动态规划 + ### 思路 抽象能力不管是在工程还是算法中都占据着绝对重要的位置。比如上题我们可以抽象为: diff --git a/problems/437.path-sum-iii.md b/problems/437.path-sum-iii.md index 869ccf0d5..ca4d95d4f 100644 --- a/problems/437.path-sum-iii.md +++ b/problems/437.path-sum-iii.md @@ -32,6 +32,10 @@ Return 3. The paths that sum to 8 are: 3. -3 -> 11 ``` +## 前置知识 + +- hashmap + ## 思路 这道题目是要我们求解出任何一个节点出发到子孙节点的路径中和为指定值。 注意这里,不一定是从根节点出发,也不一定在叶子节点结束。 diff --git a/problems/445.add-two-numbers-ii.md b/problems/445.add-two-numbers-ii.md index 3f8e2e730..ab4e0c9fe 100644 --- a/problems/445.add-two-numbers-ii.md +++ b/problems/445.add-two-numbers-ii.md @@ -19,6 +19,11 @@ Output: 7 -> 8 -> 0 -> 7 ``` +## 前置知识 + +- 链表 +- 栈 + ## 思路 由于需要从低位开始加,然后进位。 因此可以采用栈来简化操作。 diff --git a/problems/454.4-sum-ii.md b/problems/454.4-sum-ii.md index 6cadc5aa4..cdf0d4a74 100644 --- a/problems/454.4-sum-ii.md +++ b/problems/454.4-sum-ii.md @@ -26,6 +26,11 @@ The two tuples are: 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 ``` + +## 前置知识 + +- hashTable + ## 思路 如果按照常规思路去完成查找需要四层遍历,时间复杂是O(n^4), 显然是行不通的。 diff --git a/problems/455.AssignCookies.md b/problems/455.AssignCookies.md index a0e38ca49..b5b821c4f 100644 --- a/problems/455.AssignCookies.md +++ b/problems/455.AssignCookies.md @@ -35,6 +35,11 @@ https://leetcode-cn.com/problems/assign-cookies 所以你应该输出2. ``` +## 前置知识 + +- 贪心算法 +- 双指针求解 + ## 思路 贪心算法+双指针求解 diff --git a/problems/460.lfu-cache.md b/problems/460.lfu-cache.md index d9e9f30a3..5d9a44b41 100644 --- a/problems/460.lfu-cache.md +++ b/problems/460.lfu-cache.md @@ -28,6 +28,11 @@ cache.get(3); // returns 3 cache.get(4); // returns 4 ``` +## 前置知识 + +- 链表 +- HashMap + ## 思路 `本题已被收录到我的新书中,敬请期待~` diff --git a/problems/472.concatenated-words.md b/problems/472.concatenated-words.md index 409948d64..0b78f70b8 100644 --- a/problems/472.concatenated-words.md +++ b/problems/472.concatenated-words.md @@ -26,6 +26,10 @@ https://leetcode-cn.com/problems/concatenated-words/ 不需要考虑答案输出的顺序。 ``` +## 前置知识 + +- 前缀树 + ## 思路 本题我的思路是直接使用前缀树来解决。**标准的前缀树模板**我在之前的题解中提到了,感兴趣的可以到下方的相关题目中查看。 diff --git a/problems/493.reverse-pairs.md b/problems/493.reverse-pairs.md index 0470c9720..f0a0335d9 100644 --- a/problems/493.reverse-pairs.md +++ b/problems/493.reverse-pairs.md @@ -24,6 +24,12 @@ https://leetcode-cn.com/problems/reverse-pairs/description/ ``` +## 前置知识 + +- 归并排序 +- 逆序数 +- 分治 + ## 暴力法 ### 思路 diff --git a/problems/494.target-sum.md b/problems/494.target-sum.md index 6611e253f..6632c8033 100644 --- a/problems/494.target-sum.md +++ b/problems/494.target-sum.md @@ -28,6 +28,10 @@ Your output answer is guaranteed to be fitted in a 32-bit integer. ``` +## 前置知识 + +- 动态规划 + ## 思路 题目是给定一个数组,让你在数字前面添加 `+`或者`-`,使其和等于 target.