Skip to content

Commit b256ae8

Browse files
author
luzhipeng
committedMay 23, 2019

20 files changed

+620
-14
lines changed
 

‎README.en.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,17 @@ The data structures mainly includes:
9797
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
9898
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
9999
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
100+
- [0104.maximum-depth-of-binary-tree](./problems/104.maximum-depth-of-binary-tree.md) 🆕
100101
- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md)
101102
- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md)
102103
- [0136.single-number](./problems/136.single-number.md)
104+
- [0155.min-stack](./problems/155.min-stack.md) 🆕
103105
- [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)
104106
- [0172.factorial-trailing-zeroes](./problems/172.factorial-trailing-zeroes.md) 🆕
105107
- [0169.majority-element](./problems/169.majority-element.md)
106108
- [0190.reverse-bits](./problems/190.reverse-bits.md)
107109
- [0191.number-of-1-bits](./problems/191.number-of-1-bits.md)
108-
- [0198.house-robber](./problems/198.house-robber.md) 🆕
110+
- [0198.house-robber](./problems/198.house-robber.md)
109111
- [0203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md)
110112
- [0206.reverse-linked-list](./problems/206.reverse-linked-list.md)
111113
- [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md)
@@ -114,7 +116,8 @@ The data structures mainly includes:
114116
- [0283.move-zeroes](./problems/283.move-zeroes.md)
115117
- [0342.power-of-four](./problems/342.power-of-four.md) 🆕
116118
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
117-
- [0575.distribute-candies](./problems/575.distribute-candies.md) 🆕
119+
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md) 🆕
120+
- [0575.distribute-candies](./problems/575.distribute-candies.md)
118121

119122

120123
#### Medium
@@ -131,11 +134,12 @@ The data structures mainly includes:
131134
- [0047.permutations-ii](./problems/47.permutations-ii.md)
132135
- [0055.jump-game](./problems/55.jump-game.md)
133136
- [0062.unique-paths](./problems/62.unique-paths.md )
137+
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )🆕
134138
- [0075.sort-colors](./problems/75.sort-colors.md)
135-
- [0078.subsets](./problems/78.subsets.md)
139+
- [0078.subsets](./problems/78.subsets.md)
136140
- [0086.partition-list](./problems/86.partition-list.md)
137141
- [0090.subsets-ii](./problems/90.subsets-ii.md)
138-
- [0091.decode-ways](./problems/91.decode-ways.md) 🆕
142+
- [0091.decode-ways](./problems/91.decode-ways.md)
139143
- [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
140144
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
141145
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
@@ -147,8 +151,9 @@ The data structures mainly includes:
147151
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
148152
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
149153
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
150-
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
151-
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)
154+
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
155+
- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md) 🆕
156+
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
152157
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
153158
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
154159
- [0279.perfect-squares](./problems/279.perfect-squares.md)

‎README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,17 @@ leetcode 题解,记录自己的 leetcode 解题之路。
9595
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
9696
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
9797
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
98+
- [0104.maximum-depth-of-binary-tree](./problems/104.maximum-depth-of-binary-tree.md) 🆕
9899
- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md)
99100
- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md)
100101
- [0136.single-number](./problems/136.single-number.md)
102+
- [0155.min-stack](./problems/155.min-stack.md) 🆕
101103
- [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)
102104
- [0172.factorial-trailing-zeroes](./problems/172.factorial-trailing-zeroes.md) 🆕
103105
- [0169.majority-element](./problems/169.majority-element.md)
104106
- [0190.reverse-bits](./problems/190.reverse-bits.md)
105107
- [0191.number-of-1-bits](./problems/191.number-of-1-bits.md)
106-
- [0198.house-robber](./problems/198.house-robber.md) 🆕
108+
- [0198.house-robber](./problems/198.house-robber.md)
107109
- [0203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md)
108110
- [0206.reverse-linked-list](./problems/206.reverse-linked-list.md)
109111
- [0219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md)
@@ -112,7 +114,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。
112114
- [0283.move-zeroes](./problems/283.move-zeroes.md)
113115
- [0342.power-of-four](./problems/342.power-of-four.md) 🆕
114116
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
115-
- [0575.distribute-candies](./problems/575.distribute-candies.md) 🆕
117+
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md) 🆕
118+
- [0575.distribute-candies](./problems/575.distribute-candies.md)
116119

117120

118121
#### 中等难度
@@ -127,25 +130,27 @@ leetcode 题解,记录自己的 leetcode 解题之路。
127130
- [0040.combination-sum-ii](./problems/40.combination-sum-ii.md)
128131
- [0046.permutations](./problems/46.permutations.md)
129132
- [0047.permutations-ii](./problems/47.permutations-ii.md)
130-
- [0055.jump-game](./problems/55.jump-game.md) 🆕
131-
- [0062.unique-paths](./problems/62.unique-paths.md )🆕
133+
- [0055.jump-game](./problems/55.jump-game.md)
134+
- [0062.unique-paths](./problems/62.unique-paths.md )
135+
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )🆕
132136
- [0075.sort-colors](./problems/75.sort-colors.md)
133137
- [0078.subsets](./problems/78.subsets.md)
134138
- [0086.partition-list](./problems/86.partition-list.md)
135139
- [0090.subsets-ii](./problems/90.subsets-ii.md)
136-
- [0091.decode-ways](./problems/91.decode-ways.md) 🆕
140+
- [0091.decode-ways](./problems/91.decode-ways.md)
137141
- [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
138142
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
139143
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
140144
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
141145
- [0139.word-break](./problems/139.word-breakmd)
142146
- [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
143-
- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md) 🖊
147+
- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md)
144148
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕
145149
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
146150
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
147151
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
148-
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) 🖊
152+
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
153+
- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md) 🆕
149154
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
150155
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
151156
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)

‎assets/drawio/155.min-stack.drawio

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-05-23T09:51:25.572Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="RPkBK-kxfKoC4dzgXOxr" version="10.6.8" type="device"><diagram id="PFsNL-FoD6JsyvnQ05nH" name="第 1 页">7Vtbc6M2FP41nrQP3kHiYvMYO077kk47mdlu9k0B2dAFxIKI7f76SiCBQSSbbYiFd8lMYnQkdPnOd46OjpyZuY4Pv2UoDe6Ij6MZNPzDzLyZQbg0AfvLBUchcM1KsMtCvxKBRnAf/ouF0BDSIvRx3mpICYlomLaFHkkS7NGWDGUZ2bebbUnUHjVFO6wI7j0UqdK/Q58GYhVw0ch/x+EukCMDx61qYiQbi5XkAfLJ/kRkbmbmOiOEVk/xYY0jjp3EpXrv9pnaemIZTuhrXvA+fgV/Pc03H7d3hvPwGf2x8sjcqXp5QlEhFjwX+snpUWKQkSLxMe/GmJmrfRBSfJ8ij9fumdKZLKBxxEqAPYoOcUbx4dmZgnr9jDeYxJhmR9ZEvGAZYg6CMxLBfaMAIGXBCfiOkCGh813dcwMLexDIfAdKCwUlY3QgLXWDtFSpBEeHErB0w+SO0eKAa4wMJgAUnMbHJsvU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7PiInu8c1AfC83BPTXtO/rzXn9sysaPNTaj5w8mfl0alXqTp9uhqXmtSVa+qgKVdV2r+a/KAwgN27+n0G5aaX5mU9YyyRmBafd9vmQLbnyCwdXpuW88b2FpqomLi3k/BPVf7ocpSMy8T935M7nUOicA27HcjHys2/7BYXcE3//Vpbv4D</diagram></mxfile>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-05-23T12:35:30.493Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="nv6lmyas7rovaqpUcTqn" version="10.6.8" type="device"><diagram id="FilWrf6WajoPKxdyopK8" name="第 1 页">7ZxLk5pAFIV/DcupApqHLtXoJItk41SSLSUtUAHaQYxOfn0apVW6nWRSMd5jdBYMXN5fH/EcabDYqNg8VtEi/ShinluuHW8s9s5y3R5z5LApvLQFz9sVkiqLdyXnUJhmP3hbtNvqKov5srNgLUReZ4tucSbKks/qTi2qKrHuLjYXeXeviyjhRmE6i3Kz+iWL67Q9Czc81N/zLEnVnp2gv5tTRGrh9kyWaRSL9VGJjS02qoSod2PFZsTzhp3isltv8src/YFVvKzfssJnPykmT/bz4/PTwFt8+vqQDh4f2q18j/IV7x5t/aIQVGJVxrzZim2x4TrNaj5dRLNm7lq2uayldZHLKUeOttvjVc03rx6osz99KRsuCl5XL3IRtYI6BiWZdnJ94O+3pfQIvapFbYsn+w0foMiRlssfMHIRGYVYjBggIxdMRx4iIzAd+YCMGJiOAkRGYDoKDUYOOSMPTEc9REZgOuobjJjBSJ5t3QWxrCvxjY9ELipZKUUplxzOszzXSlGeJaWcnEk6XNaHDbtMms9BO6PI4rjZzUny3bY5A3y/y77vG+y9E+zdf8VeRQCoC51uTp0+sUIdSAsfolFCNPG6QaWnBGnj4bSEaOR1k0pPCdLKw2kJ0czrRpWeEqSdh9PSWwz9/2pWXZvarZpRwfLHoPznoqzb39pdNa12Z7lsMmHy70yXXFv7mPjmx+SiDaU2DHXJ1WPFnhrZj97XECvoKV1DrKCndA2xgp7SNcQKekrXECvoKZmxAsAww2nJjBX0WtJjBT0lM1a4BiUMW3sG/FqsYA5xrGCmW7XcIG9gx9n3TiMEz6umL8nWzD8st+5+IBeQZ7/ZslHz5VjS/t9uZ7mIyr/bkD9koWP5clV7uirkUMzl4Gkt5PCDbNaEV0u1N0lht8PuQcjy9nxUFVJfv4lN4XgQjM+kQ6fnd4S4v3geCdENLqpEyESgfaf4tkd7tWSIiUDPTfSUIBMBnJYQE4Gem+gpQSYCOC0h3mjQcxM9JcgbDXBauoaeQ/SUbrnvkK9uh1DlJrUzqOud7lYDh1ijHqSnD9EoIXp63a3SU4L09HBaQvT0ululpwTp6eG0hOjpdbdKTwnS08Np6ZY7DwUutVs90XnIDaKiOfndELQtCDsS+aH5kblooykRHTda23T3hjpqqP1tCrKGgkw52vdkeOIGz2UfvURMOXoWpKcEmXLgtISYcvQsSE8JMuXAaQkx5ehZkJ4SZF8mOC1dw50LekpmGrmdLBgy4h5fyi3fY8WvY0XQc4kbyowV+9De9mnrTt1bsNuCvRNdWy/bgpCRR39NiGdekC77vhnEyGO8AIucEmTkgdMSYuQxXoJFTgky8sBpCTHyGC/CIqcEGXngtGRGHnotGS/DoqakjucmHwXqUd/GCE/mjfujQIix6ayPAvW7QuyHphDP9SiQnDy8kXg77+i1zmz8Ew==</diagram></mxfile>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-05-23T09:51:28.121Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="u7Zr5KUqWn9R9PeJaYf1" version="10.6.8" type="device"><diagram id="hOtl4ipsQ4CED_UJ3B7D" name="第 1 页">7V1Rc+I2EP41PCZjW7ZsHgMhvbbXTtvMtI8ZxxbgqUE+WwRyv74yWI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BAhkvOgRzH0m4XYJS7GyokNB/GiP9jwhjki2o3CzCW0RkXhvHhcmjbvMkCjMF0aRSG4eEiFyLTTAKMHQEtFTSrQZ698UZWQk9iK0MNTBpXyyOaP1JmHr+S/A9aZSyjSg1+lS54pYzRlULFjBYqJvBHK+onX+0W9X6U+1WVxOQ+JUVJkpiR9L6gFb/yZb83RGLXXt2T6ZPX8xQylFxLBwNauArXUgzundPChwGwgRa90wKF8siBnFAzMWAgbyBG78Twgb3QTQtVNHKgxWdoMc+z4u+mxV3YDk+iSORrpogqWjlQRCNFfOMooophDhTRSJFA2m6AIt0TU5XnOlDkMxRJM37j5lnXtKzVeI1EE/c0eT0niyodSoSru2eLzRunwOQ/UKw597up1+ZcPdc1D0+b9065gXl4Qi/EHjxhd490w9n79qleNkgoDIF2pG3eQKUwBNrxtPn4CWAIsO4daYHVB1CA7q4fT7gOaxGeoLtrxxPbvPMJIePmURi6Tbew9wkup2hHWsTxrWSub55HFUKP6iaYC22E7ilWCH2tW3AOFDZCO9I2H0ynsBHa8bTZ2YKWQPvsK7TZ2YL9XT+eNntbsL/rxxN6W/OsrNhfdDuq4aizGdWb0YxLT3Qx1B7ev7rRnifvZMAKY61c8LtagmKo2C5ca2+6zzQdtNc6oVjSXqg4zqxn7dm8ABeADH7t/o3ojjfm32DzYiCRzWtz2LxIR9S7v9jPKVDQRuj2b6LbXLZT2AjtSENP8hY4rbAe2pG22ceENkK7TxT1fvahpnmEfqRtXuuDlkA/nkMCqKEJoO6HpkL9JoBGw/ZGQ9nifyhrqF+2iG3Zw39hffKUxlBx9m1X/4TFv57+IW1fdvY3c2j2Hw==</diagram></mxfile>

‎assets/problems/155.min-stack-1.png

16.5 KB
Loading

‎assets/problems/155.min-stack-2.png

57.9 KB
Loading

‎assets/problems/155.min-stack-3.png

35.2 KB
Loading
20.6 KB
Loading
25.1 KB
Loading
32.9 KB
Loading
27.6 KB
Loading

‎problems/102.binary-tree-level-order-traversal.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ return its level order traversal as:
3939

4040
- 树的基本操作- 遍历 - 层次遍历(BFS)
4141

42+
- 注意塞入null的时候,判断一下当前队列是否为空,不然会无限循环
43+
4244

4345
## 代码
4446

@@ -121,5 +123,7 @@ var levelOrder = function(root) {
121123

122124
return items;
123125
};
124-
125126
```
127+
## 相关题目
128+
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
129+
- [104.maximum-depth-of-binary-tree](./104.maximum-depth-of-binary-tree.md)

‎problems/103.binary-tree-zigzag-level-order-traversal.md

+3
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,7 @@ var zigzagLevelOrder = function(root) {
132132

133133
};
134134
```
135+
## 相关题目
136+
- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md)
137+
- [104.maximum-depth-of-binary-tree](./104.maximum-depth-of-binary-tree.md)
135138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a binary tree, find its maximum depth.
9+
10+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
11+
12+
Note: A leaf is a node with no children.
13+
14+
Example:
15+
16+
Given binary tree [3,9,20,null,null,15,7],
17+
18+
3
19+
/ \
20+
9 20
21+
/ \
22+
15 7
23+
return its depth = 3.
24+
25+
```
26+
27+
## 思路
28+
29+
由于树是一种递归的数据结构,因此用递归去解决的时候往往非常容易,这道题恰巧也是如此,
30+
用递归实现的代码如下:
31+
32+
```js
33+
var maxDepth = function(root) {
34+
if (!root) return 0;
35+
if (!root.left && !root.right) return 1;
36+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
37+
};
38+
```
39+
40+
如果使用迭代呢? 我们首先应该想到的是树的各种遍历,由于我们求的是深度,因此
41+
使用层次遍历(BFS)是非常合适的。 我们只需要记录有多少层即可。相关思路请查看[binary-tree-traversal](../thinkings/binary-tree-traversal.md)
42+
43+
## 关键点解析
44+
45+
- 队列
46+
47+
- 队列中用 Null(一个特殊元素)来划分每层
48+
49+
- 树的基本操作- 遍历 - 层次遍历(BFS)
50+
51+
## 代码
52+
53+
```js
54+
/*
55+
* @lc app=leetcode id=104 lang=javascript
56+
*
57+
* [104] Maximum Depth of Binary Tree
58+
*/
59+
/**
60+
* Definition for a binary tree node.
61+
* function TreeNode(val) {
62+
* this.val = val;
63+
* this.left = this.right = null;
64+
* }
65+
*/
66+
/**
67+
* @param {TreeNode} root
68+
* @return {number}
69+
*/
70+
var maxDepth = function(root) {
71+
if (!root) return 0;
72+
if (!root.left && !root.right) return 1;
73+
74+
// 层次遍历 BFS
75+
let cur = root;
76+
const queue = [root, null];
77+
let depth = 1;
78+
79+
while ((cur = queue.shift()) !== undefined) {
80+
if (cur === null) {
81+
// 注意⚠️: 不处理会无限循环,进而堆栈溢出
82+
if (queue.length === 0) return depth;
83+
depth++;
84+
queue.push(null);
85+
continue;
86+
}
87+
const l = cur.left;
88+
const r = cur.right;
89+
90+
if (l) queue.push(l);
91+
if (r) queue.push(r);
92+
}
93+
94+
return depth;
95+
};
96+
```
97+
## 相关题目
98+
- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md)
99+
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)

‎problems/155.min-stack.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/min-stack/description/
4+
5+
## 题目描述
6+
7+
```
8+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
9+
10+
push(x) -- Push element x onto stack.
11+
pop() -- Removes the element on top of the stack.
12+
top() -- Get the top element.
13+
getMin() -- Retrieve the minimum element in the stack.
14+
Example:
15+
MinStack minStack = new MinStack();
16+
minStack.push(-2);
17+
minStack.push(0);
18+
minStack.push(-3);
19+
minStack.getMin(); --> Returns -3.
20+
minStack.pop();
21+
minStack.top(); --> Returns 0.
22+
minStack.getMin(); --> Returns -2.
23+
24+
```
25+
26+
## 思路
27+
28+
符合直觉的方法是,每次对栈进行修改操作(push和pop)的时候更新最小值。 然后getMin只需要返回我们计算的最小值即可,
29+
top也是直接返回栈顶元素即可。 这种做法每次修改栈都需要更新最小值,因此时间复杂度是O(n).
30+
31+
![155.min-stack](../assets/problems/155.min-stack-1.png)
32+
33+
是否有更高效的算法呢?答案是有的。
34+
35+
我们每次入栈的时候,保存的不再是真正的数字,而是它与当前最小值的差(当前元素没有入栈的时候的最小值)。
36+
这样我们pop和top的时候拿到栈顶元素再加上**上一个**最小值即可。
37+
另外我们在push和pop的时候去更新min,这样getMin的时候就简单了,直接返回min。
38+
39+
> 注意上面加粗的“上一个”,不是“当前的最小值”
40+
41+
经过上面的分析,问题的关键转化为“如果求的上一个最小值”,解决这个的关键点在于利用min。
42+
43+
pop或者top的时候:
44+
45+
- 如果栈顶元素小于0,说明栈顶是当前最小的元素,它出栈会对min造成影响,我们需要去更新min。
46+
上一个最小的是“min - 栈顶元素”,我们需要将上一个最小值更新为当前的最小值
47+
48+
> 因为栈顶元素入栈的时候的通过 `栈顶元素 = 真实值 - 上一个最小的元素` 得到的,
49+
而真实值 = min, 因此可以得出`上一个最小的元素 = 真实值 -栈顶元素`
50+
51+
- 如果栈顶元素大于0,说明它对最小值`没有影响`,上一个最小值就是上上个最小值。
52+
53+
![155.min-stack-2](../assets/problems/155.min-stack-2.png)
54+
![155.min-stack-3](../assets/problems/155.min-stack-3.png)
55+
56+
## 关键点
57+
58+
- 最小栈存储的不应该是真实值,而是真实值和min的差值
59+
- top的时候涉及到对数据的还原,这里千万注意是**上一个**最小值
60+
61+
## 代码
62+
63+
```js
64+
/*
65+
* @lc app=leetcode id=155 lang=javascript
66+
*
67+
* [155] Min Stack
68+
*/
69+
/**
70+
* initialize your data structure here.
71+
*/
72+
var MinStack = function() {
73+
this.stack = [];
74+
this.min = Number.MAX_VALUE;
75+
};
76+
77+
/**
78+
* @param {number} x
79+
* @return {void}
80+
*/
81+
MinStack.prototype.push = function(x) {
82+
// update 'min'
83+
const min = this.min;
84+
if (x < this.min) {
85+
this.min = x;
86+
}
87+
return this.stack.push(x - min);
88+
};
89+
90+
/**
91+
* @return {void}
92+
*/
93+
MinStack.prototype.pop = function() {
94+
const item = this.stack.pop();
95+
const min = this.min;
96+
97+
if (item < 0) {
98+
this.min = min - item;
99+
return min;
100+
}
101+
return item + min;
102+
};
103+
104+
/**
105+
* @return {number}
106+
*/
107+
MinStack.prototype.top = function() {
108+
const item = this.stack[this.stack.length - 1];
109+
const min = this.min;
110+
111+
if (item < 0) {
112+
return min;
113+
}
114+
return item + min;
115+
};
116+
117+
/**
118+
* @return {number}
119+
*/
120+
MinStack.prototype.getMin = function() {
121+
return this.min;
122+
};
123+
124+
/**
125+
* Your MinStack object will be instantiated and called as such:
126+
* var obj = new MinStack()
127+
* obj.push(x)
128+
* obj.pop()
129+
* var param_3 = obj.top()
130+
* var param_4 = obj.getMin()
131+
*/
132+
```
133+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
9+
10+
Note:
11+
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
12+
13+
Example 1:
14+
15+
Input: root = [3,1,4,null,2], k = 1
16+
3
17+
/ \
18+
1 4
19+
\
20+
2
21+
Output: 1
22+
Example 2:
23+
24+
Input: root = [5,3,6,2,4,null,null,1], k = 3
25+
5
26+
/ \
27+
3 6
28+
/ \
29+
2 4
30+
/
31+
1
32+
Output: 3
33+
Follow up:
34+
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?
35+
36+
```
37+
38+
## 思路
39+
由于‘中序遍历一个二叉查找树(BST)的结果是一个有序数组’ ,因此我们只需要在遍历到第k个,返回当前元素即可。
40+
中序遍历相关思路请查看[binary-tree-traversal](../thinkings/binary-tree-traversal.md)
41+
42+
43+
## 关键点解析
44+
45+
- 中序遍历
46+
47+
## 代码
48+
49+
```js
50+
51+
52+
/*
53+
* @lc app=leetcode id=230 lang=javascript
54+
*
55+
* [230] Kth Smallest Element in a BST
56+
*/
57+
/**
58+
* Definition for a binary tree node.
59+
* function TreeNode(val) {
60+
* this.val = val;
61+
* this.left = this.right = null;
62+
* }
63+
*/
64+
/**
65+
* @param {TreeNode} root
66+
* @param {number} k
67+
* @return {number}
68+
*/
69+
var kthSmallest = function(root, k) {
70+
const stack = [root];
71+
let cur = root;
72+
let i = 0;
73+
74+
function insertAllLefts(cur) {
75+
while(cur && cur.left) {
76+
const l = cur.left;
77+
stack.push(l);
78+
cur = l;
79+
}
80+
}
81+
insertAllLefts(cur);
82+
83+
while(cur = stack.pop()) {
84+
i++;
85+
if (i === k) return cur.val;
86+
const r = cur.right;
87+
88+
if (r) {
89+
stack.push(r);
90+
insertAllLefts(r);
91+
}
92+
}
93+
94+
return -1;
95+
96+
97+
};
98+
```
99+
100+
## 扩展
101+
102+
这道题有一个follow up:
103+
104+
`What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently?
105+
How would you optimize the kthSmallest routine?`
106+
107+
大家可以思考一下。
108+

‎problems/371.sum-of-two-integers.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/sum-of-two-integers/description/
4+
5+
## 题目描述
6+
7+
```
8+
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
9+
10+
Example 1:
11+
12+
Input: a = 1, b = 2
13+
Output: 3
14+
Example 2:
15+
16+
Input: a = -2, b = 3
17+
Output: 1
18+
19+
```
20+
21+
## 思路
22+
23+
不能使用加减法来求加法。 我们只能朝着位元算的角度来思考了。
24+
25+
由于`异或``相同则位0,不同则位1`,因此我们可以把异或看成是一种不进位的加减法。
26+
27+
![371.sum-of-two-integers-1](../assets/problems/371.sum-of-two-integers-1.png)
28+
29+
由于```全部位1则位1,否则位0`,因此我们可以求与之后左移一位来表示进位。
30+
31+
![371.sum-of-two-integers-2](../assets/problems/371.sum-of-two-integers-2.png)
32+
33+
然后我们对上述两个元算结果递归求解即可。 递归的结束条件就是其中一个为0,我们直接返回另一个。
34+
35+
## 关键点解析
36+
37+
- 位运算
38+
- 异或是一种不进位的加减法
39+
- 求与之后左移一位来可以表示进位
40+
41+
## 代码
42+
```js
43+
/*
44+
* @lc app=leetcode id=371 lang=javascript
45+
*
46+
* [371] Sum of Two Integers
47+
*/
48+
/**
49+
* @param {number} a
50+
* @param {number} b
51+
* @return {number}
52+
*/
53+
var getSum = function(a, b) {
54+
if (a === 0) return b;
55+
56+
if (b === 0) return a;
57+
58+
return getSum(a ^ b, (a & b) << 1);
59+
};
60+
```
61+

‎problems/73.set-matrix-zeroes.md

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/set-matrix-zeroes/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
9+
10+
Example 1:
11+
12+
Input:
13+
[
14+
[1,1,1],
15+
[1,0,1],
16+
[1,1,1]
17+
]
18+
Output:
19+
[
20+
[1,0,1],
21+
[0,0,0],
22+
[1,0,1]
23+
]
24+
Example 2:
25+
26+
Input:
27+
[
28+
[0,1,2,0],
29+
[3,4,5,2],
30+
[1,3,1,5]
31+
]
32+
Output:
33+
[
34+
[0,0,0,0],
35+
[0,4,5,0],
36+
[0,3,1,0]
37+
]
38+
Follow up:
39+
40+
- A straight forward solution using O(mn) space is probably a bad idea.
41+
- A simple improvement uses O(m + n) space, but still not the best solution.
42+
- Could you devise a constant space solution?
43+
44+
```
45+
46+
## 思路
47+
48+
符合直觉的想法是,使用一个 m + n 的数组来表示每一行每一列是否”全部是 0“,
49+
先遍历一遍去构建这样的 m + n 数组,然后根据这个 m + n 数组去修改 matrix 即可。
50+
51+
![73.set-matrix-zeroes-1](../assets/problems/73.set-matrix-zeroes-1.png)
52+
53+
这样的时间复杂度 O(m \* n), 空间复杂度 O(m + n).
54+
55+
代码如下:
56+
57+
```js
58+
var setZeroes = function(matrix) {
59+
if (matrix.length === 0) return matrix;
60+
const m = matrix.length;
61+
const n = matrix[0].length;
62+
const zeroes = Array(m + n).fill(false);
63+
64+
for (let i = 0; i < m; i++) {
65+
for (let j = 0; j < n; j++) {
66+
const item = matrix[i][j];
67+
68+
if (item === 0) {
69+
zeroes[i] = true;
70+
zeroes[m + j] = true;
71+
}
72+
}
73+
}
74+
75+
for (let i = 0; i < m; i++) {
76+
if (zeroes[i]) {
77+
matrix[i] = Array(n).fill(0);
78+
}
79+
}
80+
81+
for (let i = 0; i < n; i++) {
82+
if (zeroes[m + i]) {
83+
for (let j = 0; j < m; j++) {
84+
matrix[j][i] = 0;
85+
}
86+
}
87+
}
88+
89+
return matrix;
90+
};
91+
```
92+
93+
但是这道题目还有一个follow up, 要求使用O(1)的时间复杂度。因此上述的方法就不行了。
94+
但是我们要怎么去存取这些信息(哪一行哪一列应该全部为0)呢?
95+
96+
一种思路是使用第一行第一列的数据来代替上述的zeros数组。 这样我们就不必借助额外的存储空间,空间复杂度自然就是O(1)了。
97+
98+
由于我们不能先操作第一行和第一列, 因此我们需要记录下”第一行和第一列是否全是0“这样的一个数据,最后根据这个信息去
99+
修改第一行和第一列。
100+
101+
具体步骤如下:
102+
103+
- 记录下”第一行和第一列是否全是0“这样的一个数据
104+
- 遍历除了第一行和第一列之外的所有的数据,如果是0,那就更新第一行第一列中对应的元素为0
105+
> 你可以把第一行第一列看成我们上面那种解法使用的m + n 数组。
106+
- 根据第一行第一列的数据,更新matrix
107+
- 最后根据我们最开始记录的”第一行和第一列是否全是0“去更新第一行和第一列即可
108+
109+
![73.set-matrix-zeroes-2](../assets/problems/73.set-matrix-zeroes-2.png)
110+
111+
112+
## 关键点
113+
- 使用第一行和第一列来替代我们m + n 数组
114+
- 先记录下”第一行和第一列是否全是0“这样的一个数据,否则会因为后续对第一行第一列的更新造成数据丢失
115+
- 最后更新第一行第一列
116+
## 代码
117+
118+
```js
119+
/*
120+
* @lc app=leetcode id=73 lang=javascript
121+
*
122+
* [73] Set Matrix Zeroes
123+
*/
124+
/**
125+
* @param {number[][]} matrix
126+
* @return {void} Do not return anything, modify matrix in-place instead.
127+
*/
128+
var setZeroes = function(matrix) {
129+
if (matrix.length === 0) return matrix;
130+
const m = matrix.length;
131+
const n = matrix[0].length;
132+
133+
// 时间复杂度 O(m * n), 空间复杂度 O(1)
134+
let firstRow = false; // 第一行是否应该全部为0
135+
let firstCol = false; // 第一列是否应该全部为0
136+
137+
for (let i = 0; i < m; i++) {
138+
for (let j = 0; j < n; j++) {
139+
const item = matrix[i][j];
140+
if (item === 0) {
141+
if (i === 0) {
142+
firstRow = true;
143+
}
144+
if (j === 0) {
145+
firstCol = true;
146+
}
147+
matrix[0][j] = 0;
148+
matrix[i][0] = 0;
149+
}
150+
}
151+
}
152+
153+
for (let i = 1; i < m; i++) {
154+
for (let j = 1; j < n; j++) {
155+
const item = matrix[i][j];
156+
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
157+
matrix[i][j] = 0;
158+
}
159+
}
160+
}
161+
162+
// 最后处理第一行和第一列
163+
164+
if (firstRow) {
165+
for (let i = 0; i < n; i++) {
166+
matrix[0][i] = 0;
167+
}
168+
}
169+
170+
if (firstCol) {
171+
for (let i = 0; i < m; i++) {
172+
matrix[i][0] = 0;
173+
}
174+
}
175+
176+
return matrix;
177+
};
178+
```
179+
180+
## 扩展
181+
182+
为什么选择第一行第一列,选择其他行和列可以么?为什么?

‎thinkings/binary-tree-traversal.md

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ BFS 的关键点在于如何记录每一层次是否遍历完成, 我们可以
6161
6262
3. 出栈,判断有没有右节点,有则入栈,继续执行 2
6363

64+
值得注意的是,中序遍历一个二叉查找树(BST)的结果是一个有序数组,利用这个性质有些题目可以得到简化,
65+
比如[230.kth-smallest-element-in-a-bst](../problems/230.kth-smallest-element-in-a-bst.md)
66+
6467
## 后序遍历
6568

6669
相关问题[145.binary-tree-postorder-traversal](../problems/145.binary-tree-postorder-traversal.md)

0 commit comments

Comments
 (0)
Please sign in to comment.