diff --git a/README.en.md b/README.en.md index 01ddf4981..a8f071de2 100644 --- a/README.en.md +++ b/README.en.md @@ -97,15 +97,17 @@ The data structures mainly includes: - [0020.Valid Parentheses](./problems/20.validParentheses.md) - [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md) - [0088.merge-sorted-array](./problems/88.merge-sorted-array.md) +- [0104.maximum-depth-of-binary-tree](./problems/104.maximum-depth-of-binary-tree.md) 🆕 - [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) - [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) - [0136.single-number](./problems/136.single-number.md) +- [0155.min-stack](./problems/155.min-stack.md) 🆕 - [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md) - [0172.factorial-trailing-zeroes](./problems/172.factorial-trailing-zeroes.md) 🆕 - [0169.majority-element](./problems/169.majority-element.md) - [0190.reverse-bits](./problems/190.reverse-bits.md) - [0191.number-of-1-bits](./problems/191.number-of-1-bits.md) -- [0198.house-robber](./problems/198.house-robber.md) 🆕 +- [0198.house-robber](./problems/198.house-robber.md) - [0203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md) - [0206.reverse-linked-list](./problems/206.reverse-linked-list.md) - [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md) @@ -114,7 +116,8 @@ The data structures mainly includes: - [0283.move-zeroes](./problems/283.move-zeroes.md) - [0342.power-of-four](./problems/342.power-of-four.md) 🆕 - [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md) -- [0575.distribute-candies](./problems/575.distribute-candies.md) 🆕 +- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md) 🆕 +- [0575.distribute-candies](./problems/575.distribute-candies.md) #### Medium @@ -131,11 +134,12 @@ The data structures mainly includes: - [0047.permutations-ii](./problems/47.permutations-ii.md) - [0055.jump-game](./problems/55.jump-game.md) - [0062.unique-paths](./problems/62.unique-paths.md ) +- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )🆕 - [0075.sort-colors](./problems/75.sort-colors.md) -- [0078.subsets](./problems/78.subsets.md) +- [0078.subsets](./problems/78.subsets.md) - [0086.partition-list](./problems/86.partition-list.md) - [0090.subsets-ii](./problems/90.subsets-ii.md) -- [0091.decode-ways](./problems/91.decode-ways.md) 🆕 +- [0091.decode-ways](./problems/91.decode-ways.md) - [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md) - [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md) - [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md) @@ -147,8 +151,9 @@ The data structures mainly includes: - [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md) - [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md) - [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md) -- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) -- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md) +- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) +- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md) 🆕 +- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕 - [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕 - [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md) - [0279.perfect-squares](./problems/279.perfect-squares.md) diff --git a/README.md b/README.md index 73230e3f2..acebdb814 100644 --- a/README.md +++ b/README.md @@ -95,15 +95,17 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0020.Valid Parentheses](./problems/20.validParentheses.md) - [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md) - [0088.merge-sorted-array](./problems/88.merge-sorted-array.md) +- [0104.maximum-depth-of-binary-tree](./problems/104.maximum-depth-of-binary-tree.md) 🆕 - [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md) - [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md) - [0136.single-number](./problems/136.single-number.md) +- [0155.min-stack](./problems/155.min-stack.md) 🆕 - [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md) - [0172.factorial-trailing-zeroes](./problems/172.factorial-trailing-zeroes.md) 🆕 - [0169.majority-element](./problems/169.majority-element.md) - [0190.reverse-bits](./problems/190.reverse-bits.md) - [0191.number-of-1-bits](./problems/191.number-of-1-bits.md) -- [0198.house-robber](./problems/198.house-robber.md) 🆕 +- [0198.house-robber](./problems/198.house-robber.md) - [0203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md) - [0206.reverse-linked-list](./problems/206.reverse-linked-list.md) - [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md) @@ -112,7 +114,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0283.move-zeroes](./problems/283.move-zeroes.md) - [0342.power-of-four](./problems/342.power-of-four.md) 🆕 - [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md) -- [0575.distribute-candies](./problems/575.distribute-candies.md) 🆕 +- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md) 🆕 +- [0575.distribute-candies](./problems/575.distribute-candies.md) #### 中等难度 @@ -127,25 +130,27 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - [0040.combination-sum-ii](./problems/40.combination-sum-ii.md) - [0046.permutations](./problems/46.permutations.md) - [0047.permutations-ii](./problems/47.permutations-ii.md) -- [0055.jump-game](./problems/55.jump-game.md) 🆕 -- [0062.unique-paths](./problems/62.unique-paths.md )🆕 +- [0055.jump-game](./problems/55.jump-game.md) +- [0062.unique-paths](./problems/62.unique-paths.md ) +- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )🆕 - [0075.sort-colors](./problems/75.sort-colors.md) - [0078.subsets](./problems/78.subsets.md) - [0086.partition-list](./problems/86.partition-list.md) - [0090.subsets-ii](./problems/90.subsets-ii.md) -- [0091.decode-ways](./problems/91.decode-ways.md) 🆕 +- [0091.decode-ways](./problems/91.decode-ways.md) - [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md) - [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md) - [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md) - [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md) - [0139.word-break](./problems/139.word-breakmd) - [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md) -- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md) 🖊 +- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md) - [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕 - [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md) - [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md) - [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md) -- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) 🖊 +- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) +- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md) 🆕 - [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕 - [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕 - [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md) diff --git a/assets/drawio/155.min-stack.drawio b/assets/drawio/155.min-stack.drawio new file mode 100644 index 000000000..9653e715e --- /dev/null +++ b/assets/dr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AFRLXOYqhIwRHGcpsX2i8U4igHlB/7BDBVtw57HHn540R1LNvhPOcW1qAEvbBBnYijv9j1lKL87Xgt2erLUnoPC/vDq9ZA+CkhxItWc+edvwzxiipusUcNJKmDFrmHCIc45iTMeS1eYwiNj4rr8u2BRfu+Rt5QIqIv1GkPqK47km+8WGgeQ7UTTnnfwq+EuZTaJEl9YzjkD8zxnmBXDZPNww0+1EaD5++uF8GzkCbPLS68bbrKLYEzR5jkomx4Y1JTYTsMow4fIPbU2Ub65pVV8ykrlglijn8yWOelo2NAHGCJYTb2nbLr+mhQRoyXjE2Xl2A6YQ0x9G2azdwMpvvNRuwVPb6ngvHM9uNmgkrneRolXISMt/emuznfZRVa0Goyu5J9Z41WjDVpE5OkfdlUpVldb43AzQHdvLLX5NNvWxT+hWlZunkruaHT6/Z1tjyX96Q87QMPd7Qkb0Ctj2z2avGXRm+3ldmX/XP1l0N0R6WicsVXMoeC3uouNhcO5uBzn/Q7PiInu8c1AfC83BPTXtO/rzXn9sysaPNTaj5w8mfl0alXqTp9uhqXmtSVa+qgKVdV2r+a/KAwgN27+n0G5aaX5mU9YyyRmBafd9vmQLbnyCwdXpuW88b2FpqomLi3k/BPVf7ocpSMy8T935M7nUOicA27HcjHys2/7BYXcE3//Vpbv4D \ No newline at end of file diff --git a/assets/drawio/371.sum-of-two-integers.drawio b/assets/drawio/371.sum-of-two-integers.drawio new file mode 100644 index 000000000..4241c200e --- /dev/null +++ b/assets/drawio/371.sum-of-two-integers.drawio @@ -0,0 +1 @@ +7ZxLk5pAFIV/DcupApqHLtXoJItk41SSLSUtUAHaQYxOfn0apVW6nWRSMd5jdBYMXN5fH/EcabDYqNg8VtEi/ShinluuHW8s9s5y3R5z5LApvLQFz9sVkiqLdyXnUJhmP3hbtNvqKov5srNgLUReZ4tucSbKks/qTi2qKrHuLjYXeXeviyjhRmE6i3Kz+iWL67Q9Czc81N/zLEnVnp2gv5tTRGrh9kyWaRSL9VGJjS02qoSod2PFZsTzhp3isltv8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YuQxXoJFTgky8sBpCTHyGC/CIqcEGXngtGRGHnotGS/DoqakjucmHwXqUd/GCE/mjfujQIix6ayPAvW7QuyHphDP9SiQnDy8kXg77+i1zmz8Ew== \ No newline at end of file diff --git a/assets/drawio/73.set-matrix-zeroes.drawio b/assets/drawio/73.set-matrix-zeroes.drawio new file mode 100644 index 000000000..f6774e802 --- /dev/null +++ b/assets/drawio/73.set-matrix-zeroes.drawio @@ -0,0 +1 @@ +7V1Rc+I2EP41PCZjW7ZsHgMhvbbXTtvMtI8ZxxbgqUE+WwRyv74yWIC1opfMGEtizAMDK2NZ335aaVcrMULT1e6nMi6Wv9GU5CPPSXcj9DjyvAi5/L0WvDcC3z8IFmWWHkTuSfCcfSeN0GmkmywlVetCRmnOsqItTOh6TRLWksVlSbfty+Y0b9daxAsCBM9JnEPpP1nKlk0rvPAk/0KyxVLU7OLxoWQVi4ubllTLOKXbMxGajdC0pJQdPq12U5LX2AlcDr97ulB6fLCSrNlHfvDrz7/86Xx7fNl9ycnvL+/br9txftfc5S3ON02DQ3RfEXbHH7/MdnffSUk5/IcGsHeBCiM7XudkyVY5F7j8Y8VK+i+Z0pyWXLKma37lZJ7luSSK82yx5l8T/tSEyydvpGQZx/uhKVhlaVpXM9kuM0aeizip69xycnFZSTfrlNQNcurb0zVrGOOJ76K6kYfC2QOe1fKmjbwisrsInntUCacyoSvCynd+SfMD/roPDj9qiOyHjV63J1ogp5Etzygh9B83TFwcb35SFv/Q6OsTuvOA7lygqDZgKkjPdPgDPCdBiMaP3eDphm00XQTR9BVg+tcCE1kMpucaBmZgM5iBYWBii8EE3VxhNHsFMwRgOh2DeTbkcTDnUUKSBIyPvOQ1Cvygq9FJNgC6YY4s5iwwALrBHFsMpmwAjvN6XWCK+q1EU+7n+tGE/oNFaAamoWnzjN73JbPp6UYTTumvPNbP5556rE/xKw5wRzhHpuHsW8zawDMNTeg73QRrZeuAdeMM3apb8ASAddCOc+8eVy98BnZDO842u1yydfAc3WhCn+sWWCtbB+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7TxT1fvahpnmEfqRtXuuDlkA/nkMCqKEJoO6HpkL9JoBGw/ZGQ9nifyhrqF+2iG3Zw39hffKUxlBx9m1X/4TFv57+IW1fdvY3c2j2Hw== \ No newline at end of file diff --git a/assets/problems/155.min-stack-1.png b/assets/problems/155.min-stack-1.png new file mode 100644 index 000000000..f254e1d98 Binary files /dev/null and b/assets/problems/155.min-stack-1.png differ diff --git a/assets/problems/155.min-stack-2.png b/assets/problems/155.min-stack-2.png new file mode 100644 index 000000000..610a971c0 Binary files /dev/null and b/assets/problems/155.min-stack-2.png differ diff --git a/assets/problems/155.min-stack-3.png b/assets/problems/155.min-stack-3.png new file mode 100644 index 000000000..c84ef9873 Binary files /dev/null and b/assets/problems/155.min-stack-3.png differ diff --git a/assets/problems/371.sum-of-two-integers-1.png b/assets/problems/371.sum-of-two-integers-1.png new file mode 100644 index 000000000..94c5da527 Binary files /dev/null and b/assets/problems/371.sum-of-two-integers-1.png differ diff --git a/assets/problems/371.sum-of-two-integers-2.png b/assets/problems/371.sum-of-two-integers-2.png new file mode 100644 index 000000000..c01a1b31b Binary files /dev/null and b/assets/problems/371.sum-of-two-integers-2.png differ diff --git a/assets/problems/73.set-matrix-zeroes-1.png b/assets/problems/73.set-matrix-zeroes-1.png new file mode 100644 index 000000000..de396ab2d Binary files /dev/null and b/assets/problems/73.set-matrix-zeroes-1.png differ diff --git a/assets/problems/73.set-matrix-zeroes-2.png b/assets/problems/73.set-matrix-zeroes-2.png new file mode 100644 index 000000000..28062e4eb Binary files /dev/null and b/assets/problems/73.set-matrix-zeroes-2.png differ diff --git a/problems/102.binary-tree-level-order-traversal.md b/problems/102.binary-tree-level-order-traversal.md index ef2f743ab..9e218e35e 100644 --- a/problems/102.binary-tree-level-order-traversal.md +++ b/problems/102.binary-tree-level-order-traversal.md @@ -39,6 +39,8 @@ return its level order traversal as: - 树的基本操作- 遍历 - 层次遍历(BFS) +- 注意塞入null的时候,判断一下当前队列是否为空,不然会无限循环 + ## 代码 @@ -121,5 +123,7 @@ var levelOrder = function(root) { return items; }; - ``` +## 相关题目 +- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md) +- [104.maximum-depth-of-binary-tree](./104.maximum-depth-of-binary-tree.md) diff --git a/problems/103.binary-tree-zigzag-level-order-traversal.md b/problems/103.binary-tree-zigzag-level-order-traversal.md index beeefa71d..4552a7b1e 100644 --- a/problems/103.binary-tree-zigzag-level-order-traversal.md +++ b/problems/103.binary-tree-zigzag-level-order-traversal.md @@ -132,4 +132,7 @@ var zigzagLevelOrder = function(root) { }; ``` +## 相关题目 +- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md) +- [104.maximum-depth-of-binary-tree](./104.maximum-depth-of-binary-tree.md) diff --git a/problems/104.maximum-depth-of-binary-tree.md b/problems/104.maximum-depth-of-binary-tree.md new file mode 100644 index 000000000..51584ddb8 --- /dev/null +++ b/problems/104.maximum-depth-of-binary-tree.md @@ -0,0 +1,99 @@ +## 题目地址 + +https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ + +## 题目描述 + +``` +Given a binary tree, find its maximum depth. + +The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. + +Note: A leaf is a node with no children. + +Example: + +Given binary tree [3,9,20,null,null,15,7], + + 3 + / \ + 9 20 + / \ + 15 7 +return its depth = 3. + +``` + +## 思路 + +由于树是一种递归的数据结构,因此用递归去解决的时候往往非常容易,这道题恰巧也是如此, +用递归实现的代码如下: + +```js +var maxDepth = function(root) { + if (!root) return 0; + if (!root.left && !root.right) return 1; + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); +}; +``` + +如果使用迭代呢? 我们首先应该想到的是树的各种遍历,由于我们求的是深度,因此 +使用层次遍历(BFS)是非常合适的。 我们只需要记录有多少层即可。相关思路请查看[binary-tree-traversal](../thinkings/binary-tree-traversal.md) + +## 关键点解析 + +- 队列 + +- 队列中用 Null(一个特殊元素)来划分每层 + +- 树的基本操作- 遍历 - 层次遍历(BFS) + +## 代码 + +```js +/* + * @lc app=leetcode id=104 lang=javascript + * + * [104] Maximum Depth of Binary Tree + */ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + if (!root) return 0; + if (!root.left && !root.right) return 1; + + // 层次遍历 BFS + let cur = root; + const queue = [root, null]; + let depth = 1; + + while ((cur = queue.shift()) !== undefined) { + if (cur === null) { + // 注意⚠️: 不处理会无限循环,进而堆栈溢出 + if (queue.length === 0) return depth; + depth++; + queue.push(null); + continue; + } + const l = cur.left; + const r = cur.right; + + if (l) queue.push(l); + if (r) queue.push(r); + } + + return depth; +}; +``` +## 相关题目 +- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md) +- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md) \ No newline at end of file diff --git a/problems/155.min-stack.md b/problems/155.min-stack.md new file mode 100644 index 000000000..1f8b6f11f --- /dev/null +++ b/problems/155.min-stack.md @@ -0,0 +1,133 @@ +## 题目地址 + +https://leetcode.com/problems/min-stack/description/ + +## 题目描述 + +``` +Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. + +push(x) -- Push element x onto stack. +pop() -- Removes the element on top of the stack. +top() -- Get the top element. +getMin() -- Retrieve the minimum element in the stack. +Example: +MinStack minStack = new MinStack(); +minStack.push(-2); +minStack.push(0); +minStack.push(-3); +minStack.getMin(); --> Returns -3. +minStack.pop(); +minStack.top(); --> Returns 0. +minStack.getMin(); --> Returns -2. + +``` + +## 思路 + +符合直觉的方法是,每次对栈进行修改操作(push和pop)的时候更新最小值。 然后getMin只需要返回我们计算的最小值即可, +top也是直接返回栈顶元素即可。 这种做法每次修改栈都需要更新最小值,因此时间复杂度是O(n). + +![155.min-stack](../assets/problems/155.min-stack-1.png) + +是否有更高效的算法呢?答案是有的。 + +我们每次入栈的时候,保存的不再是真正的数字,而是它与当前最小值的差(当前元素没有入栈的时候的最小值)。 +这样我们pop和top的时候拿到栈顶元素再加上**上一个**最小值即可。 +另外我们在push和pop的时候去更新min,这样getMin的时候就简单了,直接返回min。 + +> 注意上面加粗的“上一个”,不是“当前的最小值” + +经过上面的分析,问题的关键转化为“如果求的上一个最小值”,解决这个的关键点在于利用min。 + +pop或者top的时候: + +- 如果栈顶元素小于0,说明栈顶是当前最小的元素,它出栈会对min造成影响,我们需要去更新min。 +上一个最小的是“min - 栈顶元素”,我们需要将上一个最小值更新为当前的最小值 + + > 因为栈顶元素入栈的时候的通过 `栈顶元素 = 真实值 - 上一个最小的元素` 得到的, + 而真实值 = min, 因此可以得出`上一个最小的元素 = 真实值 -栈顶元素` + +- 如果栈顶元素大于0,说明它对最小值`没有影响`,上一个最小值就是上上个最小值。 + +![155.min-stack-2](../assets/problems/155.min-stack-2.png) +![155.min-stack-3](../assets/problems/155.min-stack-3.png) + +## 关键点 + +- 最小栈存储的不应该是真实值,而是真实值和min的差值 +- top的时候涉及到对数据的还原,这里千万注意是**上一个**最小值 + +## 代码 + +```js +/* + * @lc app=leetcode id=155 lang=javascript + * + * [155] Min Stack + */ +/** + * initialize your data structure here. + */ +var MinStack = function() { + this.stack = []; + this.min = Number.MAX_VALUE; +}; + +/** + * @param {number} x + * @return {void} + */ +MinStack.prototype.push = function(x) { + // update 'min' + const min = this.min; + if (x < this.min) { + this.min = x; + } + return this.stack.push(x - min); +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function() { + const item = this.stack.pop(); + const min = this.min; + + if (item < 0) { + this.min = min - item; + return min; + } + return item + min; +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function() { + const item = this.stack[this.stack.length - 1]; + const min = this.min; + + if (item < 0) { + return min; + } + return item + min; +}; + +/** + * @return {number} + */ +MinStack.prototype.getMin = function() { + return this.min; +}; + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(x) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.getMin() + */ +``` + diff --git a/problems/230.kth-smallest-element-in-a-bst.md b/problems/230.kth-smallest-element-in-a-bst.md new file mode 100644 index 000000000..345296cd7 --- /dev/null +++ b/problems/230.kth-smallest-element-in-a-bst.md @@ -0,0 +1,108 @@ +## 题目地址 + +https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ + +## 题目描述 + +``` +Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. + +Note: +You may assume k is always valid, 1 ≤ k ≤ BST's total elements. + +Example 1: + +Input: root = [3,1,4,null,2], k = 1 + 3 + / \ + 1 4 + \ + 2 +Output: 1 +Example 2: + +Input: root = [5,3,6,2,4,null,null,1], k = 3 + 5 + / \ + 3 6 + / \ + 2 4 + / + 1 +Output: 3 +Follow up: +What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? + +``` + +## 思路 +由于‘中序遍历一个二叉查找树(BST)的结果是一个有序数组’ ,因此我们只需要在遍历到第k个,返回当前元素即可。 +中序遍历相关思路请查看[binary-tree-traversal](../thinkings/binary-tree-traversal.md) + + +## 关键点解析 + +- 中序遍历 + +## 代码 + +```js + + +/* + * @lc app=leetcode id=230 lang=javascript + * + * [230] Kth Smallest Element in a BST + */ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthSmallest = function(root, k) { + const stack = [root]; + let cur = root; + let i = 0; + + function insertAllLefts(cur) { + while(cur && cur.left) { + const l = cur.left; + stack.push(l); + cur = l; + } + } + insertAllLefts(cur); + + while(cur = stack.pop()) { + i++; + if (i === k) return cur.val; + const r = cur.right; + + if (r) { + stack.push(r); + insertAllLefts(r); + } + } + + return -1; + + +}; +``` + +## 扩展 + +这道题有一个follow up: + +`What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? + How would you optimize the kthSmallest routine?` + +大家可以思考一下。 + diff --git a/problems/371.sum-of-two-integers.md b/problems/371.sum-of-two-integers.md new file mode 100644 index 000000000..a52df6a53 --- /dev/null +++ b/problems/371.sum-of-two-integers.md @@ -0,0 +1,61 @@ + +## 题目地址 +https://leetcode.com/problems/sum-of-two-integers/description/ + +## 题目描述 + +``` +Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. + +Example 1: + +Input: a = 1, b = 2 +Output: 3 +Example 2: + +Input: a = -2, b = 3 +Output: 1 + +``` + +## 思路 + +不能使用加减法来求加法。 我们只能朝着位元算的角度来思考了。 + +由于`异或`是`相同则位0,不同则位1`,因此我们可以把异或看成是一种不进位的加减法。 + +![371.sum-of-two-integers-1](../assets/problems/371.sum-of-two-integers-1.png) + +由于`与`是`全部位1则位1,否则位0`,因此我们可以求与之后左移一位来表示进位。 + +![371.sum-of-two-integers-2](../assets/problems/371.sum-of-two-integers-2.png) + +然后我们对上述两个元算结果递归求解即可。 递归的结束条件就是其中一个为0,我们直接返回另一个。 + +## 关键点解析 + +- 位运算 +- 异或是一种不进位的加减法 +- 求与之后左移一位来可以表示进位 + +## 代码 +```js +/* + * @lc app=leetcode id=371 lang=javascript + * + * [371] Sum of Two Integers + */ +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +var getSum = function(a, b) { + if (a === 0) return b; + + if (b === 0) return a; + + return getSum(a ^ b, (a & b) << 1); +}; +``` + diff --git a/problems/73.set-matrix-zeroes.md b/problems/73.set-matrix-zeroes.md new file mode 100644 index 000000000..a208ce2d8 --- /dev/null +++ b/problems/73.set-matrix-zeroes.md @@ -0,0 +1,182 @@ +## 题目地址 + +https://leetcode.com/problems/set-matrix-zeroes/description/ + +## 题目描述 + +``` +Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. + +Example 1: + +Input: +[ + [1,1,1], + [1,0,1], + [1,1,1] +] +Output: +[ + [1,0,1], + [0,0,0], + [1,0,1] +] +Example 2: + +Input: +[ + [0,1,2,0], + [3,4,5,2], + [1,3,1,5] +] +Output: +[ + [0,0,0,0], + [0,4,5,0], + [0,3,1,0] +] +Follow up: + +- A straight forward solution using O(mn) space is probably a bad idea. +- A simple improvement uses O(m + n) space, but still not the best solution. +- Could you devise a constant space solution? + +``` + +## 思路 + +符合直觉的想法是,使用一个 m + n 的数组来表示每一行每一列是否”全部是 0“, +先遍历一遍去构建这样的 m + n 数组,然后根据这个 m + n 数组去修改 matrix 即可。 + +![73.set-matrix-zeroes-1](../assets/problems/73.set-matrix-zeroes-1.png) + +这样的时间复杂度 O(m \* n), 空间复杂度 O(m + n). + +代码如下: + +```js +var setZeroes = function(matrix) { + if (matrix.length === 0) return matrix; + const m = matrix.length; + const n = matrix[0].length; + const zeroes = Array(m + n).fill(false); + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + const item = matrix[i][j]; + + if (item === 0) { + zeroes[i] = true; + zeroes[m + j] = true; + } + } + } + + for (let i = 0; i < m; i++) { + if (zeroes[i]) { + matrix[i] = Array(n).fill(0); + } + } + + for (let i = 0; i < n; i++) { + if (zeroes[m + i]) { + for (let j = 0; j < m; j++) { + matrix[j][i] = 0; + } + } + } + + return matrix; +}; +``` + +但是这道题目还有一个follow up, 要求使用O(1)的时间复杂度。因此上述的方法就不行了。 +但是我们要怎么去存取这些信息(哪一行哪一列应该全部为0)呢? + +一种思路是使用第一行第一列的数据来代替上述的zeros数组。 这样我们就不必借助额外的存储空间,空间复杂度自然就是O(1)了。 + +由于我们不能先操作第一行和第一列, 因此我们需要记录下”第一行和第一列是否全是0“这样的一个数据,最后根据这个信息去 +修改第一行和第一列。 + +具体步骤如下: + +- 记录下”第一行和第一列是否全是0“这样的一个数据 +- 遍历除了第一行和第一列之外的所有的数据,如果是0,那就更新第一行第一列中对应的元素为0 +> 你可以把第一行第一列看成我们上面那种解法使用的m + n 数组。 +- 根据第一行第一列的数据,更新matrix +- 最后根据我们最开始记录的”第一行和第一列是否全是0“去更新第一行和第一列即可 + +![73.set-matrix-zeroes-2](../assets/problems/73.set-matrix-zeroes-2.png) + + +## 关键点 +- 使用第一行和第一列来替代我们m + n 数组 +- 先记录下”第一行和第一列是否全是0“这样的一个数据,否则会因为后续对第一行第一列的更新造成数据丢失 +- 最后更新第一行第一列 +## 代码 + +```js +/* + * @lc app=leetcode id=73 lang=javascript + * + * [73] Set Matrix Zeroes + */ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + if (matrix.length === 0) return matrix; + const m = matrix.length; + const n = matrix[0].length; + + // 时间复杂度 O(m * n), 空间复杂度 O(1) + let firstRow = false; // 第一行是否应该全部为0 + let firstCol = false; // 第一列是否应该全部为0 + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + const item = matrix[i][j]; + if (item === 0) { + if (i === 0) { + firstRow = true; + } + if (j === 0) { + firstCol = true; + } + matrix[0][j] = 0; + matrix[i][0] = 0; + } + } + } + + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + const item = matrix[i][j]; + if (matrix[0][j] == 0 || matrix[i][0] == 0) { + matrix[i][j] = 0; + } + } + } + + // 最后处理第一行和第一列 + + if (firstRow) { + for (let i = 0; i < n; i++) { + matrix[0][i] = 0; + } + } + + if (firstCol) { + for (let i = 0; i < m; i++) { + matrix[i][0] = 0; + } + } + + return matrix; +}; +``` + +## 扩展 + +为什么选择第一行第一列,选择其他行和列可以么?为什么? diff --git a/thinkings/binary-tree-traversal.md b/thinkings/binary-tree-traversal.md index 75b0d28e7..fa0caaac0 100644 --- a/thinkings/binary-tree-traversal.md +++ b/thinkings/binary-tree-traversal.md @@ -61,6 +61,9 @@ BFS 的关键点在于如何记录每一层次是否遍历完成, 我们可以 3. 出栈,判断有没有右节点,有则入栈,继续执行 2 +值得注意的是,中序遍历一个二叉查找树(BST)的结果是一个有序数组,利用这个性质有些题目可以得到简化, +比如[230.kth-smallest-element-in-a-bst](../problems/230.kth-smallest-element-in-a-bst.md) + ## 后序遍历 相关问题[145.binary-tree-postorder-traversal](../problems/145.binary-tree-postorder-traversal.md)