Skip to content

Commit aea6437

Browse files
author
luzhipeng
committed
1 parent 3032758 commit aea6437

14 files changed

+542
-12
lines changed

Diff for: README.en.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -128,33 +128,38 @@ The data structures mainly includes:
128128
- [0015.3-sum](./problems/15.3-sum.md)
129129
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
130130
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
131+
- [0029.divide-two-integers](./problems/29.divide-two-integers.md) 🆕
131132
- [0039.combination-sum](./problems/39.combination-sum.md)
132133
- [0040.combination-sum-ii](./problems/40.combination-sum-ii.md)
133134
- [0046.permutations](./problems/46.permutations.md)
134135
- [0047.permutations-ii](./problems/47.permutations-ii.md)
136+
- [0048.rotate-image](./problems/48.rotate-image.md) 🆕
137+
- [0049.group-anagrams](./problems/49.group-anagrams.md) 🆕
135138
- [0055.jump-game](./problems/55.jump-game.md)
139+
- [0056.merge-intervals](./problems/56.merge-intervals.md) 🆕
136140
- [0062.unique-paths](./problems/62.unique-paths.md )
137-
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )🆕
141+
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )
138142
- [0075.sort-colors](./problems/75.sort-colors.md)
139143
- [0078.subsets](./problems/78.subsets.md)
140144
- [0086.partition-list](./problems/86.partition-list.md)
141145
- [0090.subsets-ii](./problems/90.subsets-ii.md)
142146
- [0091.decode-ways](./problems/91.decode-ways.md)
143147
- [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
144148
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
149+
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md) 🆕
145150
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
146151
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
147152
- [0139.word-break](./problems/139.word-breakmd)
148153
- [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
149154
- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md)
150-
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕
155+
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md)
151156
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
152157
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
153158
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
154159
- [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) 🆕
160+
- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md)
156161
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
157-
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
162+
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md)
158163
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
159164
- [0279.perfect-squares](./problems/279.perfect-squares.md)
160165
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) 🆕
@@ -187,8 +192,9 @@ The data structures mainly includes:
187192

188193
### Summary of Data Structures and Algorithms
189194

190-
- 🖊 [Data Structure](./thinkings/basic-data-structure.md) (Drafts)
191-
- 🖊 [Binary Tree Traversal](./thinkings/binary-tree-traversal.md)
195+
- [Data Structure](./thinkings/basic-data-structure.md) (Drafts)
196+
- [Basic Algorithm](./thinkings/basic-algorithm.md)Drafts
197+
- [Binary Tree Traversal](./thinkings/binary-tree-traversal.md)
192198
- [Dynamic Programming](./thinkings/dynamic-programming.md)
193199
- [Huffman Encode and Run Length Encode](./thinkings/run-length-encode-and-huffman-encode.md)
194200
- [Bloom Filter](./thinkings/bloom-filter.md)

Diff for: README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,38 @@ leetcode 题解,记录自己的 leetcode 解题之路。
126126
- [0015.3-sum](./problems/15.3-sum.md)
127127
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
128128
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
129+
- [0029.divide-two-integers](./problems/29.divide-two-integers.md) 🆕
129130
- [0039.combination-sum](./problems/39.combination-sum.md)
130131
- [0040.combination-sum-ii](./problems/40.combination-sum-ii.md)
131132
- [0046.permutations](./problems/46.permutations.md)
132133
- [0047.permutations-ii](./problems/47.permutations-ii.md)
134+
- [0048.rotate-image](./problems/48.rotate-image.md) 🆕
135+
- [0049.group-anagrams](./problems/49.group-anagrams.md) 🆕
133136
- [0055.jump-game](./problems/55.jump-game.md)
137+
- [0056.merge-intervals](./problems/56.merge-intervals.md) 🆕
134138
- [0062.unique-paths](./problems/62.unique-paths.md )
135-
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )🆕
139+
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )
136140
- [0075.sort-colors](./problems/75.sort-colors.md)
137141
- [0078.subsets](./problems/78.subsets.md)
138142
- [0086.partition-list](./problems/86.partition-list.md)
139143
- [0090.subsets-ii](./problems/90.subsets-ii.md)
140144
- [0091.decode-ways](./problems/91.decode-ways.md)
141145
- [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
142146
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
147+
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md) 🆕
143148
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
144149
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
145150
- [0139.word-break](./problems/139.word-breakmd)
146151
- [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
147152
- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md)
148-
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕
153+
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md)
149154
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
150155
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
151156
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
152157
- [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) 🆕
158+
- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md)
154159
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
155-
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
160+
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md)
156161
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
157162
- [0279.perfect-squares](./problems/279.perfect-squares.md)
158163
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) 🆕
@@ -183,8 +188,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
183188

184189
### 数据结构与算法的总结
185190

186-
- 🖊 [数据结构](./thinkings/basic-data-structure.md)(草稿)
187-
- 🖊 [二叉树的遍历](./thinkings/binary-tree-traversal.md)
191+
- [数据结构](./thinkings/basic-data-structure.md)(草稿)
192+
- [基础算法](./thinkings/basic-algorithm.md)(草稿)
193+
- [二叉树的遍历](./thinkings/binary-tree-traversal.md)
188194
- [动态规划](./thinkings/dynamic-programming.md)
189195
- [哈夫曼编码和游程编码](./thinkings/run-length-encode-and-huffman-encode.md)
190196
- [布隆过滤器](./thinkings/bloom-filter.md)

Diff for: assets/drawio/29.divide-two-integers.drawio

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-05-29T06:57:33.441Z" 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="QQSD99Je1HArhkPWcUTv" version="10.7.0" type="device"><diagram id="UEnr-eE_yVzsJPlaBaPp" name="第 1 页">7Zpdb5s6GMc/jS8XAebFXAIlPZPOdI7UStOuJgoOsQaYA26Snk8/m5gEsNt1W9KiJTeJefyC/fz89xsGMCp3t01Srz/RDBfAMrIdgDfAskzoI/4nLE97C/Lh3pA3JJOJjoY78j+WRkNaH0mG21FCRmnBSD02prSqcMpGtqRp6HacbEWL8VvrJMeK4S5NCtX6mWRsLVtheUf7X5jk6/7NpuvvY8qkTyxb0q6TjG4HJhgDGDWUsn2o3EW4EM7r/bLPt3wm9lCxBlfsNRms5Da4/fQ5Ltf/BPe7p6jxvtx/EBlEMZukeJQtlrVlT70LGvpYZViUYgIYbteE4bs6SUXslkPntjUrCxm9ohWTFE1bPke0oE1XFgwdD/o33N6yhn7Dg5jARUaIREzvJ1GcrB1uGN49227z4E3eDTEtMWueeBKZAckeKDugaUse2yNO15G29QCl0ydMZBfKD0UfvcwD0tE/4XRT8Xnf2wde5w1mY9eOXVbRCgv/kqKYmJKC5BV/TLmDMLeHwn2Ed+lARpQky8RrtCyPtI0xTss4DQ/LNRbOCAlSidgaINa5eKgagJeDw/bnhgOqQ1K8BGEE0PJysEDPmRkWWx21LgeHhaY43p2Ho/BQp+4/lge0Z8fDvehZxJgdD0/hYV8QD3d2PJA6rU9x1JQIb8Yb3sq2B9JvB4RnsqRdH9z2I2pF8oCLf2lLGKFaVH9PEjxQxmipYclorUPOq1aLmpe7XGx+F2WbJniR4brBacJwtqhpy1N+7fahk27EdzyGEUZL60zzla/wt1T+lm7bcy7+vmZZ5wDk8WWdCWIXBBEITNjZliBYgtgGYQgCpwv4oN/GzVm9L+54T0HVnq7VTV+lakLnDWVtqrvZq67PpmvbhPNStak5QOplDa+yft1i1pkcUb2/qDV78KuotaLOCC9A1mmLW3aqLjHWOTSdd9a5uv0/6Ny66vyVi7Lp9A2RivWNla4eI1yV/pZKt5WDvvfXunqUcdC6fdX6Ly3UZqB09UDkqvQfK72ijeB1ij5hKofItqM5lNGdypzvW6RuW+4CPxIijxEIbRCgzoI6pXdRyBfaDwMhf2HhYQPEnhgEUDdA+MtuNHBFyjDuApEYP3iuIAZ+IBLzX5GYjyGG+Lgz7Ypj5f7sV+cx6CzBaJUq3ZTHuCnCD6sTTe/QHsF1+gPHAdzDfYghXN89E9x+LHlJ8klb7+9NrMhOuHvo1ho3hNcEvzQcPz88kLK7WzGCYx3tN6TMeasK8iDaJpTL/z9WK/rVWrSb/ERMzOmSy9FITvv5/2xfm3V7K7dg3bizGcFx/3sUt0M6D35oOxcGPIFl17vOPX08D+XyvyunrZPq9wpyQssHDs9p3JANyTga435L+e9Hjj3HTdu/irtg/7ZxDbi5a0xvneVyYHTTZDgHaNYGXhy48cluQIzXBh7UHOIgzdrgF2YB/ni8YtTFDS5qwfg7</diagram></mxfile>

Diff for: assets/drawio/48.rotate-image.drawio

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-05-29T08:21:49.168Z" 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="Q9yys75j99bNvDjmcXMv" version="10.7.0" type="device"><diagram id="71IVt6DXrzH63Tv4SlmF" name="第 1 页">7Vxdb+I4FP01PBYlMQnhkbYwI+2uVKkrDfu0chMDngkxctwC++vXTuwQfGnLzLJ1LLUvxTeO7Zx7j7kfJgN0t9l/4Xi7/oPlpBhEQb4foPtBFIVoksp/SnJoJOlo1AhWnOa601HwSP8hWhho6TPNSXXSUTBWCLo9FWasLEkmTmSYc7Y77bZkxemsW7wiQPCY4QJKv9FcrPVTROOj/Cuhq7WZOUwmzZUNNp31k1RrnLNdR4RmA3THGRPNp83+jhQKPINLc9/8lavtwjgpxSU3LHL0bbGf7dff0ddFdaj+jOe/3ehRXnDxfPrAlTgYCDh7LnOiRgkG6Ha3poI8bnGmru6k0qVsLTaFbIXy45KVQmsxTHT7jhWM12OhIEAonEq5npdwQfavPlDYwiTti7ANEfwgu5gbAo2sNq3QIL07KirWonVHR0aGtWms2pGP6MkPGsCfADMCYEb+gBn3DEwEwETegBn1zTJjAObIGzABzceOwUwAmLE/YNo0dw3mGICZeAMmoLlrMFMA5tgbMG2aR673zAkAM/UHzLhnYBp3voPmxBs0bZ67RxO67f4QPU565hyF0G/3xztKwr6hCR13f0LKpHe2OfL4Wwgw3bV/FMI4yB/XHTDdOZowEPIn3wGY7hxNGAn54yHZTHfvIcFQyJ+40ma6ezRhLORPMs5munM0zfwdNAGYW0ZLQfjsRT5kpTFrCwgK3xxX6xbsDrCV4OwHMVCWrJQD3hb4iRQPrKKCslKKM6IGlxcUsDTDxe9WhycmBNt0OkwLulIXBFOKxLrVjiOXtlUr3+xXqgw03FQZJsOcbDnJsCD5cMsq2fPvuiKj1E2L4kTdt3fz6CfMIqdyYL3WknGlxmuYCgqiYfzu10IUxNBaUPx/WYvPJRqLeih1TT0Y6vnjsoRh39D0uUYT9s42YajnURriFMwRcg2mz5GeTXT3aMJIzx9v2ia6ezRhpOdP9tYG0/muCQM9fxJkgOjO0YSBnj9JCEB012iajeatQI+U+bQJikyw9gvgkT0VCym7CYZBpNt/Kf0MJ7p1v9fqqhuHTuOBcCofV4VxtcwKzXJM0mUGAkt5JclS8rRsr5jTcVGrTJKDo3WWKiUS7Jln5N3NEur8Qp1yUmBBX07XcU7ReoYHFXdf8N1hhmjWr+86mgsYKHrNdzcDCcxXRICBartrH/s/mCKMIoEpfuYcXOQc2nRUa2Ip2LXSMxZu6mxX37VG0FT8+Q5A4djK4DiP60Yw5+CP84yS/uHpc9Zh1EP79LnEDPnuPLwb+Zx6gHx3j6fPZWbId/d4+px+OMN35/snzED4k7c9w3fneMIchD8FrzN8d41nDPH0h+9x+krc7OzHHzCn4w/bE9Q3NL0ubvfONmGk6Y8nbzPduZ+UwDjTHz/eZrp7NGGU6Y8XbzPdPZowxvQnRweY7nzf9Lm8DZjuHE0YX/qTnwNMd44mjC4BmFetIZq6YFM/jN+sH16jVigVww8Lvdi60ZlZNY9T1y0zd4PCVWqMJgPa1ODe6picN56PKUaaN4to04ztI6cX1yKtihOafGwtMoEBKTBpU9yjm/r9LF2DPl8efLeqWNcjb3H2Y1XvPR3jXNZ/sks92bTaNu+RUUaGTWNJ92q3utXruV8LoV5AM1XgRPMsL8MhzVi5pHJX48NMzhjNcyyw/KfklQIKC8IpLm4yxolsKwOYcyak+IYrY7kJo3S4LVcfu+NZxhCbw83OXghwwaGJT+v4sHxW3w7PjT8PMvT0IEMc2Kbi+CDDGKZDRulQc6qhqm058unFBSbR1YAWAa3ayt/QPFfTnHV2T93hjiKjACpyPJsms+A6SoO/eEnO/DwqQteheL2lmfedNe7E8a1xaPYv</diagram></mxfile>

Diff for: assets/drawio/49.group-anagrams.drawio

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-05-29T07:51:03.040Z" 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="BsbFmJvolCeadDQuiTwE" version="10.7.0"><diagram id="vLQqbmewtO1LD4aSa5GU" name="第 1 页">3ZnRkpowFIafhkt3gADKpVq105l2uvWi1ylkId1AKERFn74HCSBGZ9bWHbNcCX+SE/Ll58wJGmielKscZ/FXHhJm2GZYGuiTYdsW8ifwUyn7Wpk4Ti1EOQ1lp05Y0wORoinVDQ1J0esoOGeCZn0x4GlKAtHTcJ7zXb/bC2f9WTMcEUVYB5ip6k8ailiuwh53+mdCo7iZ2fL8uiXBTWe5kiLGId+dSGhhoHnOuaivknJOWAWv4VKPW15pbR8sJ6l4y4DVgca/v/zg7vPraDbbf3tm/PtIRtlitpELNmyPQbzZC4ewthdV11YjQvRTXS5M7BtaOd+kIakmNKF5F1NB1hkOqtYd+AO0WCQM7iw5w5wznh/HItP0PITaqFuSC1JeXavVEgTrEZ4Qke+hSzPAk9Cl69pN2HV72HSJT7bPlRqWronayB1YuJBsb+BsK5zN/4N3B0jtM+gCCekIaaIZJEdDSMjRDJKrQHp6eno4JsfUDJP3puxvf7Ts7+iW/ccavrOubtl/oiMk3bK/pRZsWKEE6xV9FIXI+StpXreUp6R6AyljZxJmNErhNgA+BPRZRY9CNTyVDQkNw2qai+z7u3MH/Nb4DL+v4ncu4LffDb9ax/0aLn7IyprhVyvEYMD4fd3wq7VnOFz8yNUNv1rVlsPF72iX+9VqeT9c/K52uV8tog/Dxe+dHxUfjl8tz5tjYUi3vV3w/mx4c4YcFccvulPoYHlZ2TV2h8g6SJHh9N+jBMBfFCfn1DpcfwqQj4965fSqh3fOjsXLJULHY3Gly6/jkIju4rHzBIvcB1vMv4vFjht+xWX3inOTWy8GcmeOb7gw0lyBATL4naY4ynFyq4s/rrfHi6m3MN/H2+ffttti7sTc6JK7vdvdDbfdvzjHtpP/wtDiLw==</diagram></mxfile>

Diff for: assets/problems/29.divide-two-integers.png

60.6 KB
Loading

Diff for: assets/problems/48.rotate-image-1.png

18.7 KB
Loading

Diff for: assets/problems/48.rotate-image-2.png

34.5 KB
Loading

Diff for: assets/problems/49.group-anagrams.png

21.2 KB
Loading

Diff for: problems/29.divide-two-integers.md

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## 题目地址
2+
https://leetcode.com/problems/divide-two-integers/description/
3+
4+
## 题目描述
5+
```
6+
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
7+
8+
Return the quotient after dividing dividend by divisor.
9+
10+
The integer division should truncate toward zero.
11+
12+
Example 1:
13+
14+
Input: dividend = 10, divisor = 3
15+
Output: 3
16+
Example 2:
17+
18+
Input: dividend = 7, divisor = -3
19+
Output: -2
20+
Note:
21+
22+
Both dividend and divisor will be 32-bit signed integers.
23+
The divisor will never be 0.
24+
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
25+
26+
```
27+
28+
## 思路
29+
30+
符合直觉的做法是,减数一次一次减去被减数,不断更新差,直到差小于0,我们减了多少次,结果就是多少。
31+
32+
核心代码:
33+
34+
```js
35+
let acc = divisor;
36+
let count = 0;
37+
38+
while (dividend - acc >= 0) {
39+
acc += divisor;
40+
count++;
41+
}
42+
43+
return count;
44+
45+
```
46+
47+
这种做法简单直观,但是性能却比较差. 下面来介绍一种性能更好的方法。
48+
49+
![29.divide-two-integers](../assets/problems/29.divide-two-integers.png)
50+
51+
通过上面这样的分析,我们直到可以使用二分法来解决,性能有很大的提升。
52+
53+
## 关键点解析
54+
55+
- 二分查找
56+
57+
- 正负数的判断中,这样判断更简单。
58+
59+
```js
60+
const isNegative = dividend > 0 !== divisor > 0;
61+
```
62+
63+
64+
## 代码
65+
66+
```js
67+
68+
69+
/*
70+
* @lc app=leetcode id=29 lang=javascript
71+
*
72+
* [29] Divide Two Integers
73+
*/
74+
/**
75+
* @param {number} dividend
76+
* @param {number} divisor
77+
* @return {number}
78+
*/
79+
var divide = function(dividend, divisor) {
80+
if (divisor === 1) return dividend;
81+
82+
// 这种方法很巧妙,即符号相同则为正,不同则为负
83+
const isNegative = dividend > 0 !== divisor > 0;
84+
85+
const MAX_INTERGER = Math.pow(2, 31);
86+
87+
const res = helper(Math.abs(dividend), Math.abs(divisor));
88+
89+
// overflow
90+
if (res > MAX_INTERGER - 1 || res < -1 * MAX_INTERGER) {
91+
return MAX_INTERGER - 1;
92+
}
93+
94+
return isNegative ? -1 * res : res;
95+
};
96+
97+
function helper(dividend, divisor) {
98+
// 二分法
99+
if (dividend <= 0) return 0;
100+
if (dividend < divisor) return 0;
101+
if (divisor === 1) return dividend;
102+
103+
let acc = 2 * divisor;
104+
let count = 1;
105+
106+
while (dividend - acc > 0) {
107+
acc += acc;
108+
count += count;
109+
}
110+
// 直接使用位移运算,比如acc >> 1会有问题
111+
const last = dividend - Math.floor(acc / 2);
112+
113+
return count + helper(last, divisor);
114+
}
115+
```
116+
117+
## 相关题目
118+
[875.koko-eating-bananas](./875.koko-eating-bananas.md)

Diff for: problems/48.rotate-image.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/rotate-image/description/
4+
5+
## 题目描述
6+
7+
```
8+
You are given an n x n 2D matrix representing an image.
9+
10+
Rotate the image by 90 degrees (clockwise).
11+
12+
Note:
13+
14+
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
15+
16+
Example 1:
17+
18+
Given input matrix =
19+
[
20+
[1,2,3],
21+
[4,5,6],
22+
[7,8,9]
23+
],
24+
25+
rotate the input matrix in-place such that it becomes:
26+
[
27+
[7,4,1],
28+
[8,5,2],
29+
[9,6,3]
30+
]
31+
Example 2:
32+
33+
Given input matrix =
34+
[
35+
[ 5, 1, 9,11],
36+
[ 2, 4, 8,10],
37+
[13, 3, 6, 7],
38+
[15,14,12,16]
39+
],
40+
41+
rotate the input matrix in-place such that it becomes:
42+
[
43+
[15,13, 2, 5],
44+
[14, 3, 4, 1],
45+
[12, 6, 8, 9],
46+
[16, 7,10,11]
47+
]
48+
49+
```
50+
51+
## 思路
52+
53+
这道题目让我们 in-place,也就说空间复杂度要求 O(1),如果没有这个限制的话,很简单。
54+
55+
通过观察发现,我们只需要将第 i 行变成第 n - i - 1 列, 因此我们只需要保存一个原有矩阵,然后按照这个规律一个个更新即可。
56+
57+
![48.rotate-image-1](../assets/problems/48.rotate-image-1.png)
58+
59+
代码:
60+
61+
```js
62+
var rotate = function(matrix) {
63+
// 时间复杂度O(n^2) 空间复杂度O(n)
64+
const oMatrix = JSON.parse(JSON.stringify(matrix)); // clone
65+
const n = oMatrix.length;
66+
for (let i = 0; i < n; i++) {
67+
for (let j = 0; j < n; j++) {
68+
matrix[j][n - i - 1] = oMatrix[i][j];
69+
}
70+
}
71+
};
72+
```
73+
74+
如果要求空间复杂度是O(1)的话,我们可以用一个temp记录即可,这个时候就不能逐个遍历了。
75+
比如遍历到1的时候,我们把1存到temp,然后更新1的值为7。 1被换到了3的位置,我们再将3存到temp,依次类推。
76+
但是这种解法写起来比较麻烦,这里我就不写了。
77+
78+
事实上有一个更加巧妙的做法,我们可以巧妙地利用对称轴旋转达到我们的目的,如图,我们先进行一次以对角线为轴的翻转,然后
79+
再进行一次以水平轴心线为轴的翻转即可。
80+
81+
![48.rotate-image-2](../assets/problems/48.rotate-image-2.png)
82+
83+
这种做法的时间复杂度是O(n^2) ,空间复杂度是O(1)
84+
85+
## 关键点解析
86+
87+
- 矩阵旋转操作
88+
89+
## 代码
90+
91+
```js
92+
/*
93+
* @lc app=leetcode id=48 lang=javascript
94+
*
95+
* [48] Rotate Image
96+
*/
97+
/**
98+
* @param {number[][]} matrix
99+
* @return {void} Do not return anything, modify matrix in-place instead.
100+
*/
101+
var rotate = function(matrix) {
102+
// 时间复杂度O(n^2) 空间复杂度O(1)
103+
104+
// 做法: 先沿着对角线翻转,然后沿着水平线翻转
105+
const n = matrix.length;
106+
function swap(arr, [i, j], [m, n]) {
107+
const temp = arr[i][j];
108+
arr[i][j] = arr[m][n];
109+
arr[m][n] = temp;
110+
}
111+
for (let i = 0; i < n - 1; i++) {
112+
for (let j = 0; j < n - i; j++) {
113+
swap(matrix, [i, j], [n - j - 1, n - i - 1]);
114+
}
115+
}
116+
117+
for (let i = 0; i < n / 2; i++) {
118+
for (let j = 0; j < n; j++) {
119+
swap(matrix, [i, j], [n - i - 1, j]);
120+
}
121+
}
122+
};
123+
```

0 commit comments

Comments
 (0)