Skip to content

Commit 9d1c70b

Browse files
committed
feat: update lc problems
1 parent 47339b2 commit 9d1c70b

File tree

14 files changed

+84
-105
lines changed

14 files changed

+84
-105
lines changed

solution/0000-0099/0075.Sort Colors/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ul>
1414
</ul>
1515

16-
<p>必须在不使用库的sort函数的情况下解决这个问题。</p>
16+
<p>必须在不使用库内置的 sort 函数的情况下解决这个问题。</p>
1717

1818
<p>&nbsp;</p>
1919

@@ -46,7 +46,6 @@
4646
<p><strong>进阶:</strong></p>
4747

4848
<ul>
49-
<li>你可以不使用代码库中的排序函数来解决这道题吗?</li>
5049
<li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li>
5150
</ul>
5251

solution/0700-0799/0763.Partition Labels/README.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,37 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>字符串 <code>S</code> 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。</p>
9+
<p>给你一个字符串 <code>s</code> 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。</p>
1010

11-
<p> </p>
11+
<p>注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 <code>s</code> 。</p>
1212

13-
<p><strong>示例:</strong></p>
13+
<p>返回一个表示每个字符串片段的长度的列表。</p>
14+
15+
<p>&nbsp;</p>
16+
<strong class="example">示例 1:</strong>
1417

1518
<pre>
16-
<strong>输入:</strong>S = "ababcbacadefegdehijhklij"
19+
<strong>输入:</strong>s = "ababcbacadefegdehijhklij"
1720
<strong>输出:</strong>[9,7,8]
1821
<strong>解释:</strong>
19-
划分结果为 "ababcbaca", "defegde", "hijhklij"。
22+
划分结果为 "ababcbaca""defegde""hijhklij"
2023
每个字母最多出现在一个片段中。
21-
像 "ababcbacadefegde", "hijhklij" 的划分是错误的,因为划分的片段数较少。
24+
像 "ababcbacadefegde", "hijhklij" 这样的划分是错误的,因为划分的片段数较少。 </pre>
25+
26+
<p><strong class="example">示例 2:</strong></p>
27+
28+
<pre>
29+
<strong>输入:</strong>s = "eccbbbbdec"
30+
<strong>输出:</strong>[10]
2231
</pre>
2332

24-
<p> </p>
33+
<p>&nbsp;</p>
2534

2635
<p><strong>提示:</strong></p>
2736

2837
<ul>
29-
<li><code>S</code>的长度在<code>[1, 500]</code>之间。</li>
30-
<li><code>S</code>只包含小写字母 <code>'a'</code> 到 <code>'z'</code> 。</li>
38+
<li><code>1 &lt;= s.length &lt;= 500</code></li>
39+
<li><code>s</code> 仅由小写英文字母组成</li>
3140
</ul>
3241

3342
## 解法

solution/0800-0899/0885.Spiral Matrix III/README.md

+16-25
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,39 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>在&nbsp;<code>R</code>&nbsp;&nbsp;<code>C</code>&nbsp;列的矩阵上,我们从&nbsp;<code>(r0, c0)</code>&nbsp;面朝东面开始</p>
9+
<p>在 <code>rows x cols</code> 的网格上,你从单元格 <code>(rStart, cStart)</code> 面朝东面开始。网格的西北角位于第一行第一列,网格的东南角位于最后一行最后一列。</p>
1010

11-
<p>这里,网格的西北角位于第一行第一列,网格的东南角位于最后一行最后一列。</p>
11+
<p>你需要以顺时针按螺旋状行走,访问此网格中的每个位置。每当移动到网格的边界之外时,需要继续在网格之外行走(但稍后可能会返回到网格边界)。</p>
1212

13-
<p>现在,我们以顺时针按螺旋状行走,访问此网格中的每个位置。</p>
14-
15-
<p>每当我们移动到网格的边界之外时,我们会继续在网格之外行走(但稍后可能会返回到网格边界)。</p>
16-
17-
<p>最终,我们到过网格的所有&nbsp;<code>R * C</code>&nbsp;个空间。</p>
13+
<p>最终,我们到过网格的所有&nbsp;<code>rows x cols</code>&nbsp;个空间。</p>
1814

1915
<p>按照访问顺序返回表示网格位置的坐标列表。</p>
2016

2117
<p>&nbsp;</p>
2218

23-
<p><strong>示例 1:</strong></p>
24-
25-
<pre><strong>输入:</strong>R = 1, C = 4, r0 = 0, c0 = 0
19+
<p><strong class="example">示例 1:</strong></p>
20+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0885.Spiral%20Matrix%20III/images/example_1.png" style="width: 174px; height: 99px;" />
21+
<pre>
22+
<strong>输入:</strong>rows = 1, cols = 4, rStart = 0, cStart = 0
2623
<strong>输出:</strong>[[0,0],[0,1],[0,2],[0,3]]
27-
28-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0885.Spiral%20Matrix%20III/images/example_1.png" style="height: 99px; width: 174px;">
2924
</pre>
3025

31-
<p>&nbsp;</p>
32-
33-
<p><strong>示例 2:</strong></p>
34-
35-
<pre><strong>输入:</strong>R = 5, C = 6, r0 = 1, c0 = 4
26+
<p><strong class="example">示例 2:</strong></p>
27+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0885.Spiral%20Matrix%20III/images/example_2.png" style="width: 202px; height: 142px;" />
28+
<pre>
29+
<strong>输入:</strong>rows = 5, cols = 6, rStart = 1, cStart = 4
3630
<strong>输出:</strong>[[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
37-
38-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0885.Spiral%20Matrix%20III/images/example_2.png" style="height: 142px; width: 202px;">
3931
</pre>
4032

4133
<p>&nbsp;</p>
4234

4335
<p><strong>提示:</strong></p>
4436

45-
<ol>
46-
<li><code>1 &lt;= R &lt;= 100</code></li>
47-
<li><code>1 &lt;= C &lt;= 100</code></li>
48-
<li><code>0 &lt;= r0 &lt; R</code></li>
49-
<li><code>0 &lt;= c0 &lt; C</code></li>
50-
</ol>
37+
<ul>
38+
<li><code>1 &lt;= rows, cols &lt;= 100</code></li>
39+
<li><code>0 &lt;= rStart &lt; rows</code></li>
40+
<li><code>0 &lt;= cStart &lt; cols</code></li>
41+
</ul>
5142

5243
## 解法
5344

solution/0900-0999/0948.Bag of Tokens/README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,57 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>你的初始 <strong>能量</strong> 为 <code>P</code>,初始 <strong>分数</strong> 为 <code>0</code>,只有一包令牌 <code>tokens</code> 。其中 <code>tokens[i]</code> 是第 <code>i</code> 个令牌的值(下标从 0 开始)。</p>
9+
<p>你的初始 <strong>能量</strong> 为 <code>power</code>,初始 <strong>分数</strong> 为&nbsp;<code>0</code>,只有一包令牌 <code>tokens</code> 。其中 <code>tokens[i]</code> 是第 <code>i</code> 个令牌的值(下标从 0 开始)。</p>
1010

1111
<p>令牌可能的两种使用方法如下:</p>
1212

1313
<ul>
14-
<li>如果你至少有 <code>token[i]</code> 点 <strong>能量</strong> ,可以将令牌 <code>i</code> 置为正面朝上,失去 <code>token[i]</code> 点 <strong>能量</strong> ,并得到 <code>1</code> <strong>分</strong> 。</li>
15-
<li>如果我们至少有 <code>1</code> <strong>分 </strong>,可以将令牌 <code>i</code> 置为反面朝上,获得 <code>token[i]</code> 点 <strong>能量</strong> ,并失去 <code>1</code> <strong>分</strong> 。</li>
14+
<li>如果你至少有&nbsp;<code>token[i]</code>&nbsp;点 <strong>能量</strong> ,可以将令牌 <code>i</code> 置为正面朝上,失去&nbsp;<code>token[i]</code>&nbsp;点 <strong>能量</strong> ,并得到&nbsp;<code>1</code>&nbsp;<strong>分</strong> 。</li>
15+
<li>如果我们至少有&nbsp;<code>1</code>&nbsp;<strong>分 </strong>,可以将令牌 <code>i</code> 置为反面朝上,获得&nbsp;<code>token[i]</code> 点 <strong>能量</strong> ,并失去&nbsp;<code>1</code>&nbsp;<strong>分</strong> 。</li>
1616
</ul>
1717

1818
<p>每个令牌 <strong>最多</strong> 只能使用一次,使用 <strong>顺序不限</strong> ,<strong>不需</strong> 使用所有令牌。</p>
1919

2020
<p>在使用任意数量的令牌后,返回我们可以得到的最大 <strong>分数</strong> 。</p>
2121

22-
<p> </p>
22+
<p>&nbsp;</p>
2323

2424
<ol>
2525
</ol>
2626

2727
<p><strong>示例 1:</strong></p>
2828

2929
<pre>
30-
<strong>输入:</strong>tokens = [100], P = 50
30+
<strong>输入:</strong>tokens = [100], power = 50
3131
<strong>输出:</strong>0
3232
<strong>解释:</strong>无法使用唯一的令牌,因为能量和分数都太少了。</pre>
3333

3434
<p><strong>示例 2:</strong></p>
3535

3636
<pre>
37-
<strong>输入:</strong>tokens = [100,200], P = 150
37+
<strong>输入:</strong>tokens = [100,200], power = 150
3838
<strong>输出:</strong>1
39-
<strong>解释:</strong>令牌 0 正面朝上,能量变为 50,分数变为 1 。不必使用令牌 1 ,因为你无法使用它来提高分数。</pre>
39+
<strong>解释:</strong>令牌 0 正面朝上,能量变为 50,分数变为 1 。
40+
不必使用令牌 1 ,因为你无法使用它来提高分数。</pre>
4041

4142
<p><strong>示例 3:</strong></p>
4243

4344
<pre>
44-
<strong>输入:</strong>tokens = [100,200,300,400], P = 200
45+
<strong>输入:</strong>tokens = [100,200,300,400], power = 200
4546
<strong>输出:</strong>2
4647
<strong>解释:</strong>按下面顺序使用令牌可以得到 2 分:
4748
1. 令牌 0 正面朝上,能量变为 100 ,分数变为 1
4849
2. 令牌 3 正面朝下,能量变为 500 ,分数变为 0
4950
3. 令牌 1 正面朝上,能量变为 300 ,分数变为 1
5051
4. 令牌 2 正面朝上,能量变为 0 ,分数变为 2</pre>
5152

52-
<p> </p>
53+
<p>&nbsp;</p>
5354

5455
<p><strong>提示:</strong></p>
5556

5657
<ul>
57-
<li><code>0 <= tokens.length <= 1000</code></li>
58-
<li><code>0 <= tokens[i], P < 10<sup>4</sup></code></li>
58+
<li><code>0 &lt;= tokens.length &lt;= 1000</code></li>
59+
<li><code>0 &lt;= tokens[i],&nbsp;power &lt; 10<sup>4</sup></code></li>
5960
</ul>
6061

6162
## 解法

solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
<p><strong>提示:</strong></p>
5656

5757
<ul>
58-
<li><code>1 &lt;= chips.length &lt;= 100</code></li>
59-
<li><code>1 &lt;= chips[i] &lt;= 10^9</code></li>
58+
<li><code>1 &lt;= position.length &lt;= 100</code></li>
59+
<li><code>1 &lt;= position[i] &lt;= 10^9</code></li>
6060
</ul>
6161

6262
## 解法

solution/1200-1299/1221.Split a String in Balanced Strings/README.md

+11-17
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>在一个 <strong>平衡字符串</strong> 中,<code>'L'</code> 和 <code>'R'</code> 字符的数量是相同的。</p>
9+
<p><strong>平衡字符串</strong> 中,<code>'L'</code> 和 <code>'R'</code> 字符的数量是相同的。</p>
1010

11-
<p>给你一个平衡字符串&nbsp;<code>s</code>,请你将它分割成尽可能多的平衡字符串。</p>
11+
<p>给你一个平衡字符串&nbsp;<code>s</code>,请你将它分割成尽可能多的子字符串,并满足:</p>
1212

13-
<p><strong>注意:</strong>分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。</p>
13+
<ul>
14+
<li>每个子字符串都是平衡字符串。</li>
15+
</ul>
1416

1517
<p>返回可以通过分割得到的平衡字符串的 <strong>最大数量</strong> <strong>。</strong></p>
1618

@@ -27,33 +29,25 @@
2729
<p><strong>示例 2:</strong></p>
2830

2931
<pre>
30-
<strong>输入:</strong>s = "RLLLLRRRLR"
31-
<strong>输出:</strong>3
32-
<strong>解释:</strong>s 可以分割为 "RL"、"LLLRRR"、"LR" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
33-
</pre>
32+
<strong>输入:</strong>s = "RLRRRLLRLL"
33+
<strong>输出:</strong>2
34+
<strong>解释:</strong>s 可以分割为 "RL"、"RRRLLRLL",每个子字符串中都包含相同数量的 'L' 和 'R' 。
35+
注意,s 无法分割为 "RL"、"RR"、"RL"、"LR"、"LL" 因为第 2 个和第 5 个子字符串不是平衡字符串。</pre>
3436

3537
<p><strong>示例 3:</strong></p>
3638

3739
<pre>
3840
<strong>输入:</strong>s = "LLLLRRRR"
3941
<strong>输出:</strong>1
40-
<strong>解释:</strong>s 只能保持原样 "LLLLRRRR".
41-
</pre>
42-
43-
<p><strong>示例 4:</strong></p>
44-
45-
<pre>
46-
<strong>输入:</strong>s = "RLRRRLLRLL"
47-
<strong>输出:</strong>2
48-
<strong>解释:</strong>s 可以分割为 "RL"、"RRRLLRLL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
42+
<strong>解释:</strong>s 只能保持原样 "LLLLRRRR" 。
4943
</pre>
5044

5145
<p>&nbsp;</p>
5246

5347
<p><strong>提示:</strong></p>
5448

5549
<ul>
56-
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
50+
<li><code>2 &lt;= s.length &lt;= 1000</code></li>
5751
<li><code>s[i] = 'L' 或 'R'</code></li>
5852
<li><code>s</code> 是一个 <strong>平衡</strong> 字符串</li>
5953
</ul>

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/README.md

+7-12
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050

5151
**方法一:一次遍历**
5252

53-
根据题意,如果得到交替字符串 `01010101...` 所需要的操作数为 `cnt`,那么得到交替字符串 `10101010...` 所需要的操作数为 `n - cnt`
53+
根据题意,如果得到交替字符串 `01010101...` 所需要的操作数为 $cnt$,那么得到交替字符串 `10101010...` 所需要的操作数为 $n - cnt$
5454

55-
因此,我们只需要遍历一次字符串 `s`,统计出 `cnt` 的值,那么答案即为 `min(cnt, n - cnt)`
55+
因此,我们只需要遍历一次字符串 $s$,统计出 $cnt$ 的值,那么答案即为 $\min(cnt, n - cnt)$
5656

57-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 `s` 的长度。
57+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
5858

5959
<!-- tabs:start -->
6060

@@ -65,9 +65,7 @@
6565
```python
6666
class Solution:
6767
def minOperations(self, s: str) -> int:
68-
cnt = 0
69-
for i, c in enumerate(s):
70-
cnt += c == '01'[i & 1]
68+
cnt = sum(c != '01'[i & 1] for i, c in enumerate(s))
7169
return min(cnt, len(s) - cnt)
7270
```
7371

@@ -80,7 +78,7 @@ class Solution {
8078
public int minOperations(String s) {
8179
int cnt = 0, n = s.length();
8280
for (int i = 0; i < n; ++i) {
83-
cnt += (s.charAt(i) == "01".charAt(i & 1) ? 1 : 0);
81+
cnt += (s.charAt(i) != "01".charAt(i & 1) ? 1 : 0);
8482
}
8583
return Math.min(cnt, n - cnt);
8684
}
@@ -94,9 +92,7 @@ class Solution {
9492
public:
9593
int minOperations(string s) {
9694
int cnt = 0, n = s.size();
97-
for (int i = 0; i < n; ++i) {
98-
cnt += s[i] == "01"[i & 1];
99-
}
95+
for (int i = 0; i < n; ++i) cnt += s[i] != "01"[i & 1];
10096
return min(cnt, n - cnt);
10197
}
10298
};
@@ -107,9 +103,8 @@ public:
107103
```go
108104
func minOperations(s string) int {
109105
cnt := 0
110-
t := []rune("01")
111106
for i, c := range s {
112-
if t[i&1] != c {
107+
if c != []rune("01")[i&1] {
113108
cnt++
114109
}
115110
}

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/README_EN.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
```python
5353
class Solution:
5454
def minOperations(self, s: str) -> int:
55-
cnt = 0
56-
for i, c in enumerate(s):
57-
cnt += c == '01'[i & 1]
55+
cnt = sum(c != '01'[i & 1] for i, c in enumerate(s))
5856
return min(cnt, len(s) - cnt)
5957
```
6058

@@ -65,7 +63,7 @@ class Solution {
6563
public int minOperations(String s) {
6664
int cnt = 0, n = s.length();
6765
for (int i = 0; i < n; ++i) {
68-
cnt += (s.charAt(i) == "01".charAt(i & 1) ? 1 : 0);
66+
cnt += (s.charAt(i) != "01".charAt(i & 1) ? 1 : 0);
6967
}
7068
return Math.min(cnt, n - cnt);
7169
}
@@ -79,9 +77,7 @@ class Solution {
7977
public:
8078
int minOperations(string s) {
8179
int cnt = 0, n = s.size();
82-
for (int i = 0; i < n; ++i) {
83-
cnt += s[i] == "01"[i & 1];
84-
}
80+
for (int i = 0; i < n; ++i) cnt += s[i] != "01"[i & 1];
8581
return min(cnt, n - cnt);
8682
}
8783
};
@@ -92,9 +88,8 @@ public:
9288
```go
9389
func minOperations(s string) int {
9490
cnt := 0
95-
t := []rune("01")
9691
for i, c := range s {
97-
if t[i&1] != c {
92+
if c != []rune("01")[i&1] {
9893
cnt++
9994
}
10095
}

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/Solution.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ class Solution {
22
public:
33
int minOperations(string s) {
44
int cnt = 0, n = s.size();
5-
for (int i = 0; i < n; ++i) {
6-
cnt += s[i] == "01"[i & 1];
7-
}
5+
for (int i = 0; i < n; ++i) cnt += s[i] != "01"[i & 1];
86
return min(cnt, n - cnt);
97
}
108
};

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/Solution.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
func minOperations(s string) int {
22
cnt := 0
3-
t := []rune("01")
43
for i, c := range s {
5-
if t[i&1] != c {
4+
if c != []rune("01")[i&1] {
65
cnt++
76
}
87
}

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public int minOperations(String s) {
33
int cnt = 0, n = s.length();
44
for (int i = 0; i < n; ++i) {
5-
cnt += (s.charAt(i) == "01".charAt(i & 1) ? 1 : 0);
5+
cnt += (s.charAt(i) != "01".charAt(i & 1) ? 1 : 0);
66
}
77
return Math.min(cnt, n - cnt);
88
}

0 commit comments

Comments
 (0)