Skip to content

Commit 96e5023

Browse files
authored
feat: add weekly contest 422 (#3706)
1 parent 7c00420 commit 96e5023

File tree

29 files changed

+1118
-58
lines changed

29 files changed

+1118
-58
lines changed

solution/0000-0099/0008.String to Integer (atoi)/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tags:
2222

2323
<ol>
2424
<li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li>
25-
<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li>
25+
<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity if neither present.</li>
2626
<li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li>
2727
<li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li>
2828
</ol>

solution/0200-0299/0209.Minimum Size Subarray Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tags:
5454
<ul>
5555
<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>
5656
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
57-
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
57+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
5858
</ul>
5959

6060
<p>&nbsp;</p>

solution/0700-0799/0729.My Calendar I/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ tags:
2424

2525
<p>当两个日程安排有一些时间上的交叉时(例如两个日程安排都在同一时间内),就会产生 <strong>重复预订</strong> 。</p>
2626

27-
<p>日程可以用一对整数 <code>start</code> 和 <code>end</code> 表示,这里的时间是半开区间,即 <code>[start, end)</code>, 实数&nbsp;<code>x</code> 的范围为, &nbsp;<code>start &lt;= x &lt; end</code> 。</p>
27+
<p>日程可以用一对整数 <code>startTime</code> 和 <code>endTime</code> 表示,这里的时间是半开区间,即 <code>[startTime, endTime)</code>, 实数&nbsp;<code>x</code> 的范围为, &nbsp;<code>startTime &lt;= x &lt; endTime</code> 。</p>
2828

2929
<p>实现 <code>MyCalendar</code> 类:</p>
3030

3131
<ul>
3232
<li><code>MyCalendar()</code> 初始化日历对象。</li>
33-
<li><code>boolean book(int start, int end)</code> 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 <code>true</code> 。否则,返回 <code>false</code>&nbsp;并且不要将该日程安排添加到日历中。</li>
33+
<li><code>boolean book(int startTime, int endTime)</code> 如果可以将日程安排成功添加到日历中而不会导致重复预订,返回 <code>true</code> 。否则,返回 <code>false</code>&nbsp;并且不要将该日程安排添加到日历中。</li>
3434
</ul>
3535

3636
<p>&nbsp;</p>

solution/0700-0799/0731.My Calendar II/README.md

+25-21
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,47 @@ tags:
2121

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

24-
<p>实现一个 <code>MyCalendar</code> 类来存放你的日程安排。如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排。</p>
24+
<p>实现一个程序来存放你的日程安排。如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排。</p>
2525

26-
<p><code>MyCalendar</code> 有一个 <code>book(int start, int end)</code>方法。它意味着在 <code>start</code> 到 <code>end</code> 时间内增加一个日程安排,注意,这里的时间是半开区间,即 <code>[start, end)</code>, 实数&nbsp;<code>x</code> 的范围为, &nbsp;<code>start &lt;= x &lt; end</code>。</p>
26+
<p>当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生 <strong>三重预订</strong>。</p>
2727

28-
<p>当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生三重预订。</p>
28+
<p>事件能够用一对整数&nbsp;<code>startTime</code>&nbsp;&nbsp;<code>endTime</code>&nbsp;表示,在一个半开区间的时间&nbsp;<code>[startTime, endTime)</code>&nbsp;上预定。实数&nbsp;<code>x</code> 的范围为&nbsp;&nbsp;<code>startTime &lt;= x &lt; endTime</code>。</p>
2929

30-
<p>每次调用 <code>MyCalendar.book</code>方法时,如果可以将日程安排成功添加到日历中而不会导致三重预订,返回 <code>true</code>。否则,返回 <code>false</code> 并且不要将该日程安排添加到日历中。</p>
30+
<p>实现&nbsp;<code>MyCalendarTwo</code> 类:</p>
3131

32-
<p>请按照以下步骤调用<code>MyCalendar</code> 类: <code>MyCalendar cal = new MyCalendar();</code> <code>MyCalendar.book(start, end)</code></p>
32+
<ul>
33+
<li><code>MyCalendarTwo()</code>&nbsp;初始化日历对象。</li>
34+
<li><code>boolean book(int startTime, int endTime)</code>&nbsp;如果可以将日程安排成功添加到日历中而不会导致三重预订,返回 <code>true</code>。否则,返回 <code>false</code> 并且不要将该日程安排添加到日历中。</li>
35+
</ul>
3336

3437
<p>&nbsp;</p>
3538

36-
<p><strong class="example">示例:</strong></p>
39+
<p><strong class="example">示例 1:</strong></p>
3740

3841
<pre>
39-
MyCalendar();
40-
MyCalendar.book(10, 20); // returns true
41-
MyCalendar.book(50, 60); // returns true
42-
MyCalendar.book(10, 40); // returns true
43-
MyCalendar.book(5, 15); // returns false
44-
MyCalendar.book(5, 10); // returns true
45-
MyCalendar.book(25, 55); // returns true
46-
<strong>解释:</strong>
47-
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
48-
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
49-
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
50-
第六个日程安排(25,55)可以添加至日历中,因为时间 [25,40] 将和第三个日程安排双重预订;
51-
时间 [40,50] 将单独预订,时间 [50,55)将和第二个日程安排双重预订。
42+
<strong>输入:</strong>
43+
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
44+
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
45+
<strong>输出:</strong>
46+
[null, true, true, true, false, true, true]
47+
48+
<strong>解释:</strong>
49+
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
50+
myCalendarTwo.book(10, 20); // 返回 True,能够预定该日程。
51+
myCalendarTwo.book(50, 60); // 返回 True,能够预定该日程。
52+
myCalendarTwo.book(10, 40); // 返回 True,该日程能够被重复预定。
53+
myCalendarTwo.book(5, 15); // 返回 False,该日程导致了三重预定,所以不能预定。
54+
myCalendarTwo.book(5, 10); // 返回 True,能够预定该日程,因为它不使用已经双重预订的时间 10。
55+
myCalendarTwo.book(25, 55); // 返回 True,能够预定该日程,因为时间段 [25, 40) 将被第三个日程重复预定,时间段 [40, 50) 将被单独预定,而时间段 [50, 55) 将被第二个日程重复预定。
5256
</pre>
5357

5458
<p>&nbsp;</p>
5559

5660
<p><strong>提示:</strong></p>
5761

5862
<ul>
59-
<li>每个测试用例,调用&nbsp;<code>MyCalendar.book</code>&nbsp;函数最多不超过&nbsp;<code>1000</code>次。</li>
60-
<li>调用函数&nbsp;<code>MyCalendar.book(start, end)</code>时,&nbsp;<code>start</code> 和&nbsp;<code>end</code> 的取值范围为&nbsp;<code>[0, 10^9]</code>。</li>
63+
<li><code>0 &lt;= start &lt; end &lt;= 10<sup>9</sup></code></li>
64+
<li>最多调用&nbsp;<code>book</code>&nbsp;1000 次。</li>
6165
</ul>
6266

6367
<!-- description:end -->

solution/2200-2299/2222.Number of Ways to Select Buildings/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<pre>
4242
<strong>Input:</strong> s = &quot;001101&quot;
4343
<strong>Output:</strong> 6
44-
<strong>Explanation:</strong>
44+
<strong>Explanation:</strong>
4545
The following sets of indices selected are valid:
4646
- [0,2,4] from &quot;<u><strong>0</strong></u>0<strong><u>1</u></strong>1<strong><u>0</u></strong>1&quot; forms &quot;010&quot;
4747
- [0,3,4] from &quot;<u><strong>0</strong></u>01<u><strong>10</strong></u>1&quot; forms &quot;010&quot;

solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ tags:
4141
<strong>输入:</strong>s = "100011001", k = 3
4242
<strong>输出:</strong>"11001"
4343
<strong>解释:</strong>示例中共有 7 个美丽子字符串:
44-
1. 子字符串 "<em><strong>100011</strong></em>001" 。
45-
2. 子字符串 "<strong><em>1000110</em></strong>01" 。
46-
3. 子字符串 "<strong><em>100011001</em></strong>" 。
47-
4. 子字符串 "1<strong><em>00011001</em></strong>" 。
48-
5. 子字符串 "10<strong><em>0011001</em></strong>" 。
49-
6. 子字符串 "100<em><strong>011001</strong></em>" 。
50-
7. 子字符串 "1000<strong><em>11001</em></strong>" 。
44+
1. 子字符串 "<u>100011</u>001" 。
45+
2. 子字符串 "<u>1000110</u>01" 。
46+
3. 子字符串 "<u>10001100</u>1" 。
47+
4. 子字符串 "1<u>00011001</u>" 。
48+
5. 子字符串 "10<u>0011001</u>" 。
49+
6. 子字符串 "100<u>011001</u>" 。
50+
7. 子字符串 "1000<u>11001</u>" 。
5151
最短美丽子字符串的长度是 5 。
5252
长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。
5353
</pre>
@@ -58,9 +58,9 @@ tags:
5858
<strong>输入:</strong>s = "1011", k = 2
5959
<strong>输出:</strong>"11"
6060
<strong>解释:</strong>示例中共有 3 个美丽子字符串:
61-
1. 子字符串 "<em><strong>101</strong></em>1" 。
62-
2. 子字符串 "1<em><strong>011</strong></em>" 。
63-
3. 子字符串 "10<em><strong>11</strong></em>" 。
61+
1. 子字符串 "<u>101</u>1" 。
62+
2. 子字符串 "1<u>011</u>" 。
63+
3. 子字符串 "10<u>11</u>" 。
6464
最短美丽子字符串的长度是 2 。
6565
长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。
6666
</pre>

solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ rating: 1352
66
source: 第 405 场周赛 Q2
77
tags:
88
- 位运算
9-
- 递归
109
- 字符串
10+
- 回溯
1111
---
1212

1313
<!-- problem:start -->

solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ rating: 1352
66
source: Weekly Contest 405 Q2
77
tags:
88
- Bit Manipulation
9-
- Recursion
109
- String
10+
- Backtracking
1111
---
1212

1313
<!-- problem:start -->

solution/3300-3399/3322.Premier League Table Ranking III/README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ tags:
8686
<p><strong>输出:</strong></p>
8787

8888
<pre>
89-
+------------+---------+-------------------+--------+-----------------+------+
90-
| season_id | team_id | team_name | points | goal_difference | rank |
91-
+------------+---------+-------------------+--------+-----------------+------+
92-
| 2021 | 1 | Manchester City | 93 | 73 | 1 |
93-
| 2021 | 2 | Liverpool | 92 | 68 | 2 |
94-
| 2021 | 3 | Chelsea | 74 | 43 | 3 |
95-
| 2021 | 4 | Tottenham | 71 | 29 | 4 |
96-
| 2021 | 5 | Arsenal | 69 | 13 | 5 |
97-
| 2022 | 1 | Manchester City | 89 | 61 | 1 |
98-
| 2022 | 2 | Arsenal | 84 | 45 | 2 |
99-
| 2022 | 3 | Manchester United | 75 | 15 | 3 |
100-
| 2022 | 4 | Newcastle | 71 | 35 | 4 |
101-
| 2022 | 5 | Liverpool | 67 | 28 | 5 |
102-
+------------+---------+-------------------+--------+-----------------+------+
89+
+------------+---------+-------------------+--------+-----------------+----------+
90+
| season_id | team_id | team_name | points | goal_difference | position |
91+
+------------+---------+-------------------+--------+-----------------+----------+
92+
| 2021 | 1 | Manchester City | 93 | 73 | 1 |
93+
| 2021 | 2 | Liverpool | 92 | 68 | 2 |
94+
| 2021 | 3 | Chelsea | 74 | 43 | 3 |
95+
| 2021 | 4 | Tottenham | 71 | 29 | 4 |
96+
| 2021 | 5 | Arsenal | 69 | 13 | 5 |
97+
| 2022 | 1 | Manchester City | 89 | 61 | 1 |
98+
| 2022 | 2 | Arsenal | 84 | 45 | 2 |
99+
| 2022 | 3 | Manchester United | 75 | 15 | 3 |
100+
| 2022 | 4 | Newcastle | 71 | 35 | 4 |
101+
| 2022 | 5 | Liverpool | 67 | 28 | 5 |
102+
+------------+---------+-------------------+--------+-----------------+----------+
103103
</pre>
104104

105105
<p><strong>解释:</strong></p>

solution/3300-3399/3339.Find the Number of K-Even Arrays/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ tags:
2424
<li><code>(arr[i] * arr[i + 1]) - arr[i] - arr[i + 1]</code>&nbsp;是偶数。</li>
2525
</ul>
2626

27-
<p>返回大小为 <code>n</code>&nbsp;的满足&nbsp;<strong>K 偶数</strong> 的数组的数量,其中所有元素的范围在&nbsp;<code>[1, m]</code>。</p>
27+
<p>返回长度为 <code>n</code>&nbsp;的满足&nbsp;<strong>K 偶数</strong> 的数组的数量,其中所有元素的范围在&nbsp;<code>[1, m]</code>。</p>
2828

2929
<p>因为答案可能很大,返回答案对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;取模。</p>
3030

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3340.Check%20Balanced%20String/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3340. 检查平衡字符串](https://leetcode.cn/problems/check-balanced-string)
10+
11+
[English Version](/solution/3300-3399/3340.Check%20Balanced%20String/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个仅由数字 0 - 9 组成的字符串 <code>num</code>。如果偶数下标处的数字之和等于奇数下标处的数字之和,则认为该数字字符串是一个 <b>平衡字符串</b>。</p>
18+
19+
<p>如果 <code>num</code> 是一个 <strong>平衡字符串</strong>,则返回 <code>true</code>;否则,返回 <code>false</code>。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>输入:</strong>num<span class="example-io"> = "1234"</span></p>
27+
28+
<p><strong>输出:</strong><span class="example-io">false</span></p>
29+
30+
<p><strong>解释:</strong></p>
31+
32+
<ul>
33+
<li>偶数下标处的数字之和为 <code>1 + 3 = 4</code>,奇数下标处的数字之和为 <code>2 + 4 = 6</code>。</li>
34+
<li>由于 4 不等于 6,<code>num</code> 不是平衡字符串。</li>
35+
</ul>
36+
</div>
37+
38+
<p><strong class="example">示例 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>输入:</strong>num<span class="example-io"> = "24123"</span></p>
42+
43+
<p><strong>输出:</strong>true</p>
44+
45+
<p><strong>解释:</strong></p>
46+
47+
<ul>
48+
<li>偶数下标处的数字之和为 <code>2 + 1 + 3 = 6</code>,奇数下标处的数字之和为 <code>4 + 2 = 6</code>。</li>
49+
<li>由于两者相等,<code>num</code> 是平衡字符串。</li>
50+
</ul>
51+
</div>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>2 &lt;= num.length &lt;= 100</code></li>
59+
<li><code>num</code> 仅由数字 0 - 9 组成。</li>
60+
</ul>
61+
62+
<!-- description:end -->
63+
64+
## 解法
65+
66+
<!-- solution:start -->
67+
68+
### 方法一:模拟
69+
70+
我们可以用一个长度为 $2$ 的数组 $f$ 来记录偶数下标和奇数下标的数字之和,然后遍历字符串 $\textit{nums}$,根据下标的奇偶性将数字加到对应的位置上,最后判断 $f[0]$ 是否等于 $f[1]$ 即可。
71+
72+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
73+
74+
<!-- tabs:start -->
75+
76+
#### Python3
77+
78+
```python
79+
class Solution:
80+
def isBalanced(self, num: str) -> bool:
81+
f = [0, 0]
82+
for i, x in enumerate(map(int, num)):
83+
f[i & 1] += x
84+
return f[0] == f[1]
85+
```
86+
87+
#### Java
88+
89+
```java
90+
class Solution {
91+
public boolean isBalanced(String num) {
92+
int[] f = new int[2];
93+
for (int i = 0; i < num.length(); ++i) {
94+
f[i & 1] += num.charAt(i) - '0';
95+
}
96+
return f[0] == f[1];
97+
}
98+
}
99+
```
100+
101+
#### C++
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
bool isBalanced(string num) {
107+
int f[2]{};
108+
for (int i = 0; i < num.size(); ++i) {
109+
f[i & 1] += num[i] - '0';
110+
}
111+
return f[0] == f[1];
112+
}
113+
};
114+
```
115+
116+
#### Go
117+
118+
```go
119+
func isBalanced(num string) bool {
120+
f := [2]int{}
121+
for i, c := range num {
122+
f[i&1] += int(c - '0')
123+
}
124+
return f[0] == f[1]
125+
}
126+
```
127+
128+
#### TypeScript
129+
130+
```ts
131+
function isBalanced(num: string): boolean {
132+
const f = [0, 0];
133+
for (let i = 0; i < num.length; ++i) {
134+
f[i & 1] += +num[i];
135+
}
136+
return f[0] === f[1];
137+
}
138+
```
139+
140+
<!-- tabs:end -->
141+
142+
<!-- solution:end -->
143+
144+
<!-- problem:end -->

0 commit comments

Comments
 (0)