Skip to content

Commit c480c3b

Browse files
author
Shuo
authored
Merge pull request #832 from openset/develop
A: new
2 parents 6312cd5 + 6b7fcec commit c480c3b

File tree

117 files changed

+2838
-1575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+2838
-1575
lines changed

Diff for: README.md

+29-306
Large diffs are not rendered by default.

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/openset/leetcode
22

3-
go 1.14
3+
go 1.15

Diff for: problems/accounts-merge/README.md

+30-18
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,42 @@
1111

1212
## [721. Accounts Merge (Medium)](https://leetcode.com/problems/accounts-merge "账户合并")
1313

14-
<p>Given a list <code>accounts</code>, each element <code>accounts[i]</code> is a list of strings, where the first element <code>accounts[i][0]</code> is a <i>name</i>, and the rest of the elements are <i>emails</i> representing emails of the account.</p>
14+
<p>Given a list of <code>accounts</code> where each element <code>accounts[i]</code> is a list of strings, where the first element <code>accounts[i][0]</code> is a name, and the rest of the elements are <strong>emails</strong> representing emails of the account.</p>
1515

16-
<p>Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.</p>
16+
<p>Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.</p>
1717

18-
<p>After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails <b>in sorted order</b>. The accounts themselves can be returned in any order.</p>
18+
<p>After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails <strong>in sorted order</strong>. The accounts themselves can be returned in <strong>any order</strong>.</p>
1919

20-
<p><b>Example 1:</b><br />
21-
<pre style="white-space: pre-wrap">
22-
<b>Input:</b>
23-
accounts = [["John", "[email protected]", "[email protected]"], ["John", "[email protected]"], ["John", "[email protected]", "[email protected]"], ["Mary", "[email protected]"]]
24-
<b>Output:</b> [["John", '[email protected]', '[email protected]', '[email protected]'], ["John", "[email protected]"], ["Mary", "[email protected]"]]
25-
<b>Explanation:</b>
26-
The first and third John's are the same person as they have the common email "[email protected]".
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> accounts = [[&quot;John&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;John&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Mary&quot;,&quot;[email protected]&quot;],[&quot;John&quot;,&quot;[email protected]&quot;]]
25+
<strong>Output:</strong> [[&quot;John&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Mary&quot;,&quot;[email protected]&quot;],[&quot;John&quot;,&quot;[email protected]&quot;]]
26+
<strong>Explanation:</strong>
27+
The first and third John&#39;s are the same person as they have the common email &quot;[email protected]&quot;.
2728
The second John and Mary are different people as none of their email addresses are used by other accounts.
28-
We could return these lists in any order, for example the answer [['Mary', '[email protected]'], ['John', '[email protected]'],
29-
['John', '[email protected]', '[email protected]', '[email protected]']] would still be accepted.
29+
We could return these lists in any order, for example the answer [[&#39;Mary&#39;, &#39;[email protected]&#39;], [&#39;John&#39;, &#39;[email protected]&#39;],
30+
[&#39;John&#39;, &#39;[email protected]&#39;, &#39;[email protected]&#39;, &#39;[email protected]&#39;]] would still be accepted.
3031
</pre>
31-
</p>
3232

33-
<p><b>Note:</b>
34-
<li>The length of <code>accounts</code> will be in the range <code>[1, 1000]</code>.</li>
35-
<li>The length of <code>accounts[i]</code> will be in the range <code>[1, 10]</code>.</li>
36-
<li>The length of <code>accounts[i][j]</code> will be in the range <code>[1, 30]</code>.</li>
37-
</p>
33+
<p><strong>Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> accounts = [[&quot;Gabe&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Kevin&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Ethan&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Hanzo&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Fern&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;]]
37+
<strong>Output:</strong> [[&quot;Ethan&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Gabe&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Hanzo&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Kevin&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;],[&quot;Fern&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;,&quot;[email protected]&quot;]]
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= accounts.length &lt;= 1000</code></li>
45+
<li><code>2 &lt;= accounts[i].length &lt;= 10</code></li>
46+
<li><code>1 &lt;= accounts[i][j] &lt;= 30</code></li>
47+
<li><code>accounts[i][0]</code> consists of English letters.</li>
48+
<li><code>accounts[i][j] (for j &gt; 0)</code> is a valid email.</li>
49+
</ul>
3850

3951
### Related Topics
4052
[[Depth-first Search](../../tag/depth-first-search/README.md)]

Diff for: problems/add-digits/README.md

+25-8
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,36 @@
1111

1212
## [258. Add Digits (Easy)](https://leetcode.com/problems/add-digits "各位相加")
1313

14-
<p>Given a non-negative integer <code>num</code>, repeatedly add all its digits until the result has only one digit.</p>
14+
<p>Given an integer <code>num</code>, repeatedly add all its digits until the result has only one digit, and return it.</p>
1515

16-
<p><strong>Example:</strong></p>
16+
<p>&nbsp;</p>
17+
<p><strong>Example 1:</strong></p>
1718

1819
<pre>
19-
<strong>Input:</strong> <code>38</code>
20-
<strong>Output:</strong> 2
21-
<strong>Explanation: </strong>The process is like: <code>3 + 8 = 11</code>, <code>1 + 1 = 2</code>.
22-
&nbsp; Since <code>2</code> has only one digit, return it.
20+
<strong>Input:</strong> num = 38
21+
<strong>Output:</strong> 2
22+
<strong>Explanation:</strong> The process is
23+
38 --&gt; 3 + 8 --&gt; 11
24+
11 --&gt; 1 + 1 --&gt; 2
25+
Since 2 has only one digit, return it.
2326
</pre>
2427

25-
<p><b>Follow up:</b><br />
26-
Could you do it without any loop/recursion in O(1) runtime?</p>
28+
<p><strong>Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> num = 0
32+
<strong>Output:</strong> 0
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>0 &lt;= num &lt;= 2<sup>31</sup> - 1</code></li>
40+
</ul>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Follow up:</strong> Could you do it without any loop/recursion in <code>O(1)</code> runtime?</p>
2744

2845
### Related Topics
2946
[[Math](../../tag/math/README.md)]

Diff for: problems/add-one-row-to-tree/README.md

+29-53
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,44 @@
1111

1212
## [623. Add One Row to Tree (Medium)](https://leetcode.com/problems/add-one-row-to-tree "在二叉树中增加一行")
1313

14-
<p>Given the root of a binary tree, then value <code>v</code> and depth <code>d</code>, you need to add a row of nodes with value <code>v</code> at the given depth <code>d</code>. The root node is at depth 1. </p>
14+
<p>Given the <code>root</code> of a binary tree and two integers <code>val</code> and <code>depth</code>, add a row of nodes with value <code>val</code> at the given depth <code>depth</code>.</p>
1515

16-
<p>The adding rule is: given a positive integer depth <code>d</code>, for each NOT null tree nodes <code>N</code> in depth <code>d-1</code>, create two tree nodes with value <code>v</code> as <code>N's</code> left subtree root and right subtree root. And <code>N's</code> <b>original left subtree</b> should be the left subtree of the new left subtree root, its <b>original right subtree</b> should be the right subtree of the new right subtree root. If depth <code>d</code> is 1 that means there is no depth d-1 at all, then create a tree node with value <b>v</b> as the new root of the whole original tree, and the original tree is the new root's left subtree.</p>
16+
<p>Note that the <code>root</code> node is at depth <code>1</code>.</p>
1717

18-
<p><b>Example 1:</b><br />
19-
<pre>
20-
<b>Input:</b>
21-
A binary tree as following:
22-
4
23-
/ \
24-
2 6
25-
/ \ /
26-
3 1 5
27-
28-
<b>v = 1</b>
29-
30-
<b>d = 2</b>
18+
<p>The adding rule is:</p>
3119

32-
<b>Output:</b>
33-
4
34-
/ \
35-
1 1
36-
/ \
37-
2 6
38-
/ \ /
39-
3 1 5
20+
<ul>
21+
<li>Given the integer <code>depth</code>, for each not null tree node <code>cur</code> at the depth <code>depth - 1</code>, create two tree nodes with value <code>val</code> as <code>cur</code>&#39;s left subtree root and right subtree root.</li>
22+
<li><code>cur</code>&#39;s original left subtree should be the left subtree of the new left subtree root.</li>
23+
<li><code>cur</code>&#39;s original right subtree should be the right subtree of the new right subtree root.</li>
24+
<li>If <code>depth == 1</code> that means there is no depth <code>depth - 1</code> at all, then create a tree node with value <code>val</code> as the new root of the whole original tree, and the original tree is the new root&#39;s left subtree.</li>
25+
</ul>
4026

27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg" style="width: 500px; height: 231px;" />
30+
<pre>
31+
<strong>Input:</strong> root = [4,2,6,3,1,5], val = 1, depth = 2
32+
<strong>Output:</strong> [4,1,1,2,null,null,6,3,1,5]
4133
</pre>
42-
</p>
43-
4434

45-
<p><b>Example 2:</b><br />
35+
<p><strong>Example 2:</strong></p>
36+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg" style="width: 500px; height: 277px;" />
4637
<pre>
47-
<b>Input:</b>
48-
A binary tree as following:
49-
4
50-
/
51-
2
52-
/ \
53-
3 1
54-
55-
<b>v = 1</b>
56-
57-
<b>d = 3</b>
58-
59-
<b>Output:</b>
60-
4
61-
/
62-
2
63-
/ \
64-
1 1
65-
/ \
66-
3 1
38+
<strong>Input:</strong> root = [4,2,null,3,1], val = 1, depth = 3
39+
<strong>Output:</strong> [4,2,null,1,1,3,null,null,1]
6740
</pre>
68-
</p>
6941

70-
<p><b>Note:</b><br>
71-
<ol>
72-
<li>The given d is in range [1, maximum depth of the given tree + 1].</li>
73-
<li>The given binary tree has at least one tree node.</li>
74-
</ol>
75-
</p>
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
47+
<li>The depth of the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
48+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
49+
<li><code>-10<sup>5</sup> &lt;= val &lt;= 10<sup>5</sup></code></li>
50+
<li><code>1 &lt;= depth &lt;= the depth of tree + 1</code></li>
51+
</ul>
7652

7753
### Related Topics
7854
[[Tree](../../tag/tree/README.md)]

Diff for: problems/best-time-to-buy-and-sell-stock-with-transaction-fee/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
4242
<p><strong>Constraints:</strong></p>
4343

4444
<ul>
45-
<li><code>1 &lt; prices.length &lt;= 5 * 10<sup>4</sup></code></li>
46-
<li><code>0 &lt; prices[i], fee &lt; 5 * 10<sup>4</sup></code></li>
45+
<li><code>1 &lt;= prices.length &lt;= 5 * 10<sup>4</sup></code></li>
46+
<li><code>1 &lt;= prices[i] &lt; 5 * 10<sup>4</sup></code></li>
47+
<li><code>0 &lt;= fee &lt; 5 * 10<sup>4</sup></code></li>
4748
</ul>
4849

4950
### Related Topics

Diff for: problems/binary-tree-paths/README.md

+20-13
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,33 @@
1111

1212
## [257. Binary Tree Paths (Easy)](https://leetcode.com/problems/binary-tree-paths "二叉树的所有路径")
1313

14-
<p>Given a binary tree, return all root-to-leaf paths.</p>
14+
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p>
1515

16-
<p><strong>Note:</strong>&nbsp;A leaf is a node with no children.</p>
17-
18-
<p><strong>Example:</strong></p>
16+
<p>A <strong>leaf</strong> is a node with no children.</p>
1917

18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg" style="width: 207px; height: 293px;" />
2021
<pre>
21-
<strong>Input:</strong>
22-
23-
1
24-
/ \
25-
2 3
26-
\
27-
5
22+
<strong>Input:</strong> root = [1,2,3,null,5]
23+
<strong>Output:</strong> [&quot;1-&gt;2-&gt;5&quot;,&quot;1-&gt;3&quot;]
24+
</pre>
2825

29-
<strong>Output:</strong> [&quot;1-&gt;2-&gt;5&quot;, &quot;1-&gt;3&quot;]
26+
<p><strong>Example 2:</strong></p>
3027

31-
<strong>Explanation:</strong> All root-to-leaf paths are: 1-&gt;2-&gt;5, 1-&gt;3
28+
<pre>
29+
<strong>Input:</strong> root = [1]
30+
<strong>Output:</strong> [&quot;1&quot;]
3231
</pre>
3332

33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li>The number of nodes in the tree is in the range <code>[1, 100]</code>.</li>
38+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
39+
</ul>
40+
3441
### Related Topics
3542
[[Tree](../../tag/tree/README.md)]
3643
[[Depth-first Search](../../tag/depth-first-search/README.md)]

Diff for: problems/binary-trees-with-factors/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@
3838
<ul>
3939
<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
4040
<li><code>2 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
41+
<li>All the values of <code>arr</code> are <strong>unique</strong>.</li>
4142
</ul>

Diff for: problems/cells-with-odd-values-in-a-matrix/README.md

+19-10
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,49 @@
1111

1212
## [1252. Cells with Odd Values in a Matrix (Easy)](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix "奇数值单元格的数目")
1313

14-
<p>Given&nbsp;<code>n</code>&nbsp;and&nbsp;<code>m</code>&nbsp;which are the dimensions of a matrix initialized by zeros and given an array <code>indices</code>&nbsp;where <code>indices[i] = [ri, ci]</code>. For each pair of <code>[ri, ci]</code>&nbsp;you have to increment all cells in row <code>ri</code> and column <code>ci</code>&nbsp;by 1.</p>
14+
<p>There is an <code>m x n</code> matrix that is initialized to all <code>0</code>&#39;s. There is also a 2D array <code>indices</code> where each <code>indices[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> represents a <strong>0-indexed location</strong> to perform some increment operations on the matrix.</p>
1515

16-
<p>Return <em>the number of cells with odd values</em> in the matrix after applying the increment to all <code>indices</code>.</p>
16+
<p>For each location <code>indices[i]</code>, do <strong>both</strong> of the following:</p>
17+
18+
<ol>
19+
<li>Increment <strong>all</strong> the cells on row <code>r<sub>i</sub></code>.</li>
20+
<li>Increment <strong>all</strong> the cells on column <code>c<sub>i</sub></code>.</li>
21+
</ol>
22+
23+
<p>Given <code>m</code>, <code>n</code>, and <code>indices</code>, return <em>the <strong>number of odd-valued cells</strong> in the matrix after applying the increment to all locations in </em><code>indices</code>.</p>
1724

1825
<p>&nbsp;</p>
1926
<p><strong>Example 1:</strong></p>
2027
<img alt="" src="https://assets.leetcode.com/uploads/2019/10/30/e1.png" style="width: 600px; height: 118px;" />
2128
<pre>
22-
<strong>Input:</strong> n = 2, m = 3, indices = [[0,1],[1,1]]
29+
<strong>Input:</strong> m = 2, n = 3, indices = [[0,1],[1,1]]
2330
<strong>Output:</strong> 6
2431
<strong>Explanation:</strong> Initial matrix = [[0,0,0],[0,0,0]].
2532
After applying first increment it becomes [[1,2,1],[0,1,0]].
26-
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.
33+
The final matrix is [[1,3,1],[1,3,1]], which contains 6 odd numbers.
2734
</pre>
2835

2936
<p><strong>Example 2:</strong></p>
3037
<img alt="" src="https://assets.leetcode.com/uploads/2019/10/30/e2.png" style="width: 600px; height: 150px;" />
3138
<pre>
32-
<strong>Input:</strong> n = 2, m = 2, indices = [[1,1],[0,0]]
39+
<strong>Input:</strong> m = 2, n = 2, indices = [[1,1],[0,0]]
3340
<strong>Output:</strong> 0
34-
<strong>Explanation:</strong> Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.
41+
<strong>Explanation:</strong> Final matrix = [[2,2],[2,2]]. There are no odd numbers in the final matrix.
3542
</pre>
3643

3744
<p>&nbsp;</p>
3845
<p><strong>Constraints:</strong></p>
3946

4047
<ul>
41-
<li><code>1 &lt;= n &lt;= 50</code></li>
42-
<li><code>1 &lt;= m &lt;= 50</code></li>
48+
<li><code>1 &lt;= m, n &lt;= 50</code></li>
4349
<li><code>1 &lt;= indices.length &lt;= 100</code></li>
44-
<li><code>0 &lt;= indices[i][0] &lt;&nbsp;n</code></li>
45-
<li><code>0 &lt;= indices[i][1] &lt;&nbsp;m</code></li>
50+
<li><code>0 &lt;= r<sub>i</sub> &lt; m</code></li>
51+
<li><code>0 &lt;= c<sub>i</sub> &lt; n</code></li>
4652
</ul>
4753

54+
<p>&nbsp;</p>
55+
<p><strong>Follow up:</strong> Could you solve this in <code>O(n + m + indices.length)</code> time with only <code>O(n + m)</code> extra space?</p>
56+
4857
### Related Topics
4958
[[Array](../../tag/array/README.md)]
5059

Diff for: problems/check-if-a-string-contains-all-binary-codes-of-size-k/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<p>Given a binary string <code>s</code> and an integer <code>k</code>.</p>
1515

16-
<p>Return <em>True</em> if every&nbsp;binary code&nbsp;of length <code>k</code> is a substring of <code>s</code>. Otherwise, return <em>False</em>.</p>
16+
<p>Return <code>true</code> <em>if every binary code of length</em> <code>k</code> <em>is a substring of</em> <code>s</code>. Otherwise, return <code>false</code>.</p>
1717

1818
<p>&nbsp;</p>
1919
<p><strong>Example 1:</strong></p>
@@ -58,8 +58,8 @@
5858
<p><strong>Constraints:</strong></p>
5959

6060
<ul>
61-
<li><code>1 &lt;= s.length &lt;= 5 * 10^5</code></li>
62-
<li><code>s</code> consists of 0&#39;s and 1&#39;s only.</li>
61+
<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>5</sup></code></li>
62+
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
6363
<li><code>1 &lt;= k &lt;= 20</code></li>
6464
</ul>
6565

0 commit comments

Comments
 (0)