Skip to content

Commit 324b28c

Browse files
authored
Merge branch 'doocs:main' into main
2 parents e37f6b1 + e8d6a74 commit 324b28c

File tree

334 files changed

+48068
-34422
lines changed

Some content is hidden

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

334 files changed

+48068
-34422
lines changed

images/starcharts.svg

Lines changed: 33636 additions & 33312 deletions
Loading

solution/0000-0099/0032.Longest Valid Parentheses/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>给你一个只包含 <code>'('</code>&nbsp;和 <code>')'</code>&nbsp;的字符串,找出最长有效(格式正确且连续)括号<span data-keyword="substring">子串</span>的长度。</p>
21+
<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号 <span data-keyword="substring">子串</span> 的长度。</p>
22+
23+
<p>左右括号匹配,即每个左括号都有对应的右括号将其闭合的字符串是格式正确的,比如&nbsp;<code>"(()())"</code>。</p>
2224

2325
<p>&nbsp;</p>
2426

solution/0000-0099/0033.Search in Rotated Sorted Array/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tags:
1919

2020
<p>整数数组 <code>nums</code> 按升序排列,数组中的值 <strong>互不相同</strong> 。</p>
2121

22-
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code>(<code>0 &lt;= k &lt; nums.length</code>)上进行了 <strong>旋转</strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0 开始</strong> 计数)。例如, <code>[0,1,2,4,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为&nbsp;<code>[4,5,6,7,0,1,2]</code> 。</p>
22+
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code>(<code>0 &lt;= k &lt; nums.length</code>)上进行了 <strong>向左旋转</strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0 开始</strong> 计数)。例如, <code>[0,1,2,4,5,6,7]</code> 下标&nbsp;<code>3</code>&nbsp;上向左旋转后可能变为&nbsp;<code>[4,5,6,7,0,1,2]</code> 。</p>
2323

2424
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,如果 <code>nums</code> 中存在这个目标值 <code>target</code> ,则返回它的下标,否则返回&nbsp;<code>-1</code>&nbsp;。</p>
2525

solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tags:
1919

2020
<p>There is an integer array <code>nums</code> sorted in ascending order (with <strong>distinct</strong> values).</p>
2121

22-
<p>Prior to being passed to your function, <code>nums</code> is <strong>possibly rotated</strong> at an unknown pivot index <code>k</code> (<code>1 &lt;= k &lt; nums.length</code>) such that the resulting array is <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code> (<strong>0-indexed</strong>). For example, <code>[0,1,2,4,5,6,7]</code> might be rotated at pivot index <code>3</code> and become <code>[4,5,6,7,0,1,2]</code>.</p>
22+
<p>Prior to being passed to your function, <code>nums</code> is <strong>possibly left rotated</strong> at an unknown index <code>k</code> (<code>1 &lt;= k &lt; nums.length</code>) such that the resulting array is <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code> (<strong>0-indexed</strong>). For example, <code>[0,1,2,4,5,6,7]</code> might be left rotated by&nbsp;<code>3</code>&nbsp;indices and become <code>[4,5,6,7,0,1,2]</code>.</p>
2323

2424
<p>Given the array <code>nums</code> <strong>after</strong> the possible rotation and an integer <code>target</code>, return <em>the index of </em><code>target</code><em> if it is in </em><code>nums</code><em>, or </em><code>-1</code><em> if it is not in </em><code>nums</code>.</p>
2525

solution/0000-0099/0045.Jump Game II/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ tags:
2020

2121
<p>给定一个长度为 <code>n</code> 的 <strong>0 索引</strong>整数数组 <code>nums</code>。初始位置为 <code>nums[0]</code>。</p>
2222

23-
<p>每个元素 <code>nums[i]</code> 表示从索引 <code>i</code> 向后跳转的最大长度。换句话说,如果你在 <code>nums[i]</code> 处,你可以跳转到任意 <code>nums[i + j]</code> 处:</p>
23+
<p>每个元素 <code>nums[i]</code> 表示从索引 <code>i</code> 向后跳转的最大长度。换句话说,如果你在索引&nbsp;<code>i</code>&nbsp;处,你可以跳转到任意 <code>(i + j)</code> 处</p>
2424

2525
<ul>
26-
<li><code>0 &lt;= j &lt;= nums[i]</code>&nbsp;</li>
26+
<li><code>0 &lt;= j &lt;= nums[i]</code>&nbsp;</li>
2727
<li><code>i + j &lt; n</code></li>
2828
</ul>
2929

30-
<p>返回到达&nbsp;<code>nums[n - 1]</code> 的最小跳跃次数。生成的测试用例可以到达 <code>nums[n - 1]</code>。</p>
30+
<p>返回到达&nbsp;<code>n - 1</code>&nbsp;的最小跳跃次数。测试用例保证可以到达 <code>n - 1</code>。</p>
3131

3232
<p>&nbsp;</p>
3333

@@ -54,7 +54,7 @@ tags:
5454
<ul>
5555
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
5656
<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
57-
<li>题目保证可以到达&nbsp;<code>nums[n-1]</code></li>
57+
<li>题目保证可以到达&nbsp;<code>n - 1</code></li>
5858
</ul>
5959

6060
<!-- description:end -->

solution/0000-0099/0045.Jump Game II/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>. You are initially positioned at <code>nums[0]</code>.</p>
21+
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>. You are initially positioned at&nbsp;index 0.</p>
2222

23-
<p>Each element <code>nums[i]</code> represents the maximum length of a forward jump from index <code>i</code>. In other words, if you are at <code>nums[i]</code>, you can jump to any <code>nums[i + j]</code> where:</p>
23+
<p>Each element <code>nums[i]</code> represents the maximum length of a forward jump from index <code>i</code>. In other words, if you are at index <code>i</code>, you can jump to any index <code>(i + j)</code>&nbsp;where:</p>
2424

2525
<ul>
2626
<li><code>0 &lt;= j &lt;= nums[i]</code> and</li>
2727
<li><code>i + j &lt; n</code></li>
2828
</ul>
2929

30-
<p>Return <em>the minimum number of jumps to reach </em><code>nums[n - 1]</code>. The test cases are generated such that you can reach <code>nums[n - 1]</code>.</p>
30+
<p>Return <em>the minimum number of jumps to reach index </em><code>n - 1</code>. The test cases are generated such that you can reach index&nbsp;<code>n - 1</code>.</p>
3131

3232
<p>&nbsp;</p>
3333
<p><strong class="example">Example 1:</strong></p>

solution/0000-0099/0079.Word Search/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,46 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
23+
<p>给定一个&nbsp;<code>m x n</code> 二维字符网格&nbsp;<code>board</code> 和一个字符串单词&nbsp;<code>word</code> 。如果&nbsp;<code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
2424

2525
<p>单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p>
2626

27-
<p> </p>
27+
<p>&nbsp;</p>
2828

2929
<p><strong>示例 1:</strong></p>
3030
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word2.jpg" style="width: 322px; height: 242px;" />
3131
<pre>
32-
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
32+
<strong>输入:</strong>board = [['A','B','C','E'],['S','F','C','S'],['A','D','E','E']], word = "ABCCED"
3333
<strong>输出:</strong>true
3434
</pre>
3535

3636
<p><strong>示例 2:</strong></p>
3737
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word-1.jpg" style="width: 322px; height: 242px;" />
3838
<pre>
39-
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
39+
<strong>输入:</strong>board = [['A','B','C','E'],['S','F','C','S'],['A','D','E','E']], word = "SEE"
4040
<strong>输出:</strong>true
4141
</pre>
4242

4343
<p><strong>示例 3:</strong></p>
4444
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word3.jpg" style="width: 322px; height: 242px;" />
4545
<pre>
46-
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
46+
<strong>输入:</strong>board = [['A','B','C','E'],['S','F','C','S'],['A','D','E','E']], word = "ABCB"
4747
<strong>输出:</strong>false
4848
</pre>
4949

50-
<p> </p>
50+
<p>&nbsp;</p>
5151

5252
<p><strong>提示:</strong></p>
5353

5454
<ul>
5555
<li><code>m == board.length</code></li>
5656
<li><code>n = board[i].length</code></li>
57-
<li><code>1 <= m, n <= 6</code></li>
58-
<li><code>1 <= word.length <= 15</code></li>
57+
<li><code>1 &lt;= m, n &lt;= 6</code></li>
58+
<li><code>1 &lt;= word.length &lt;= 15</code></li>
5959
<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li>
6060
</ul>
6161

62-
<p> </p>
62+
<p>&nbsp;</p>
6363

6464
<p><strong>进阶:</strong>你可以使用搜索剪枝的技术来优化解决方案,使其在 <code>board</code> 更大的情况下可以更快解决问题?</p>
6565

solution/0100-0199/0190.Reverse Bits/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ tags:
1919

2020
<p>颠倒给定的 32 位无符号整数的二进制位。</p>
2121

22-
<p><strong>提示:</strong></p>
23-
24-
<ul>
25-
<li>请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。</li>
26-
<li>在 Java 中,编译器使用<a href="https://baike.baidu.com/item/二进制补码/5295284" target="_blank">二进制补码</a>记法来表示有符号整数。因此,在 <strong>示例 2</strong>&nbsp;中,输入表示有符号整数 <code>-3</code>,输出表示有符号整数 <code>-1073741825</code>。</li>
27-
</ul>
28-
2922
<p>&nbsp;</p>
3023

3124
<p><strong class="example">示例 1:</strong></p>

solution/0100-0199/0190.Reverse Bits/README_EN.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>Reverse bits of a given 32 bits unsigned integer.</p>
21-
22-
<p><strong>Note:</strong></p>
23-
24-
<ul>
25-
<li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer&#39;s internal binary representation is the same, whether it is signed or unsigned.</li>
26-
<li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2&#39;s complement notation</a>. Therefore, in <strong class="example">Example 2</strong>&nbsp;below, the input represents the signed integer <code>-3</code> and the output represents the signed integer <code>-1073741825</code>.</li>
27-
</ul>
20+
<p>Reverse bits of a given 32 bits signed integer.</p>
2821

2922
<p>&nbsp;</p>
3023
<p><strong class="example">Example 1:</strong></p>

solution/0200-0299/0231.Power of Two/README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ tags:
6767

6868
### 方法一:位运算
6969

70-
$\texttt{n\&(n-1)}$ 可将最后一个二进制形式的 $n$ 的最后一位 $1$ 移除,若移除后为 $0$,说明 $n$ 是 $2$ 的幂。
70+
根据位运算的性质,执行 $\texttt{n\&(n-1)}$ 可以消去二进制形式的 $n$ 的最后一位 $1$。因此,如果 $n \gt 0$,并且满足 $\texttt{n\&(n-1)}$ 结果为 $0$,则说明 $n$ 是 $2$ 的幂。
71+
72+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
7173

7274
<!-- tabs:start -->
7375

@@ -116,6 +118,16 @@ function isPowerOfTwo(n: number): boolean {
116118
}
117119
```
118120

121+
#### Rust
122+
123+
```rust
124+
impl Solution {
125+
pub fn is_power_of_two(n: i32) -> bool {
126+
n > 0 && (n & (n - 1)) == 0
127+
}
128+
}
129+
```
130+
119131
#### JavaScript
120132

121133
```js
@@ -134,11 +146,11 @@ var isPowerOfTwo = function (n) {
134146

135147
<!-- solution:start -->
136148

137-
### 方法二:lowbit
149+
### 方法二:Lowbit
138150

139-
$\texttt{n\&(-n)}$ 可以得到 $n$ 的最后一位 $1$ 表示的十进制数,若与 $n$ 相等,说明 $n$ 是 $2$ 的幂。
151+
根据 $\text{lowbit}$ 的定义,我们知道 $\text{lowbit}(x) = x \& (-x)$,可以得到 $n$ 的最后一位 $1$ 表示的十进制数。因此,如果 $n > 0$,并且满足 $\text{lowbit}(n)$ 等于 $n$,则说明 $n$ 是 $2$ 的幂。
140152

141-
注意:要满足 $n$ 是 $2$ 的幂次方,需要保证 $n$ 大于 $0$。
153+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
142154

143155
<!-- tabs:start -->
144156

@@ -183,7 +195,17 @@ func isPowerOfTwo(n int) bool {
183195

184196
```ts
185197
function isPowerOfTwo(n: number): boolean {
186-
return n > 0 && (n & (n - 1)) === 0;
198+
return n > 0 && n === (n & -n);
199+
}
200+
```
201+
202+
#### Rust
203+
204+
```rust
205+
impl Solution {
206+
pub fn is_power_of_two(n: i32) -> bool {
207+
n > 0 && n == (n & (-n))
208+
}
187209
}
188210
```
189211

@@ -195,7 +217,7 @@ function isPowerOfTwo(n: number): boolean {
195217
* @return {boolean}
196218
*/
197219
var isPowerOfTwo = function (n) {
198-
return n > 0 && n == (n & -n);
220+
return n > 0 && n === (n & -n);
199221
};
200222
```
201223

0 commit comments

Comments
 (0)