Skip to content

Commit be05c9b

Browse files
authored
feat: update lc problems (#3023)
1 parent 38b5291 commit be05c9b

File tree

36 files changed

+218
-190
lines changed

36 files changed

+218
-190
lines changed

lcof2/剑指 Offer II 020. 回文子字符串的个数/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ class Solution {
167167
var i = i
168168
var j = j
169169
let chars = Array(s)
170-
170+
171171
while i >= 0 && j < chars.count && chars[i] == chars[j] {
172172
cnt += 1
173173
i -= 1
174174
j += 1
175175
}
176-
176+
177177
return cnt
178178
}
179179
}

lcof2/剑指 Offer II 021. 删除链表的倒数第 n 个结点/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,18 @@ class Solution {
255255
let dummy = ListNode(0, head)
256256
var fast: ListNode? = dummy
257257
var slow: ListNode? = dummy
258-
258+
259259
var n = n
260260
while n > 0 {
261261
fast = fast?.next
262262
n -= 1
263263
}
264-
264+
265265
while fast?.next != nil {
266266
slow = slow?.next
267267
fast = fast?.next
268268
}
269-
269+
270270
slow?.next = slow?.next?.next
271271
return dummy.next
272272
}

lcof2/剑指 Offer II 022. 链表中环的入口节点/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ class Solution {
288288
func detectCycle(_ head: ListNode?) -> ListNode? {
289289
var fast = head
290290
var slow = head
291-
291+
292292
while fast != nil && fast?.next != nil {
293293
slow = slow?.next
294294
fast = fast?.next?.next
295-
295+
296296
if slow === fast {
297297
var ans = head
298298
while ans !== slow {

solution/0300-0399/0324.Wiggle Sort II/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0324.Wiggle%20Sort%20II/README.md
55
tags:
6+
- 贪心
67
- 数组
78
- 分治
89
- 快速选择

solution/0300-0399/0324.Wiggle Sort II/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0324.Wiggle%20Sort%20II/README_EN.md
55
tags:
6+
- Greedy
67
- Array
78
- Divide and Conquer
89
- Quickselect

solution/0300-0399/0399.Evaluate Division/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tags:
88
- 并查集
99
-
1010
- 数组
11+
- 字符串
1112
- 最短路
1213
---
1314

solution/0300-0399/0399.Evaluate Division/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tags:
88
- Union Find
99
- Graph
1010
- Array
11+
- String
1112
- Shortest Path
1213
---
1314

solution/0400-0499/0422.Valid Word Square/README.md

+4-29
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,10 @@ tags:
9191
class Solution:
9292
def validWordSquare(self, words: List[str]) -> bool:
9393
m = len(words)
94-
n = max(len(w) for w in words)
95-
if m != n:
96-
return False
97-
for j in range(n):
98-
if words[j] != "".join(w[j] for w in words if j < len(w)):
99-
return False
94+
for i, w in enumerate(words):
95+
for j, c in enumerate(w):
96+
if j >= m or i >= len(words[j]) or c != words[j][i]:
97+
return False
10098
return True
10199
```
102100

@@ -179,27 +177,4 @@ function validWordSquare(words: string[]): boolean {
179177

180178
<!-- solution:end -->
181179

182-
<!-- solution:start -->
183-
184-
### 方法二
185-
186-
<!-- tabs:start -->
187-
188-
#### Python3
189-
190-
```python
191-
class Solution:
192-
def validWordSquare(self, words: List[str]) -> bool:
193-
m = len(words)
194-
for i, w in enumerate(words):
195-
for j, c in enumerate(w):
196-
if j >= m or i >= len(words[j]) or c != words[j][i]:
197-
return False
198-
return True
199-
```
200-
201-
<!-- tabs:end -->
202-
203-
<!-- solution:end -->
204-
205180
<!-- problem:end -->

solution/0400-0499/0422.Valid Word Square/README_EN.md

+11-30
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ Therefore, it is NOT a valid word square.
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Iterative Check
77+
78+
We observe that if $words[i][j] \neq words[j][i]$, we can directly return `false`.
79+
80+
Therefore, we only need to iterate through each row, and then check whether each row satisfies $words[i][j] = words[j][i]$. Note that if the index is out of bounds, we also directly return `false`.
81+
82+
The time complexity is $O(n^2)$, where $n$ is the length of `words`. The space complexity is $O(1)`.
7783

7884
<!-- tabs:start -->
7985

@@ -83,12 +89,10 @@ Therefore, it is NOT a valid word square.
8389
class Solution:
8490
def validWordSquare(self, words: List[str]) -> bool:
8591
m = len(words)
86-
n = max(len(w) for w in words)
87-
if m != n:
88-
return False
89-
for j in range(n):
90-
if words[j] != "".join(w[j] for w in words if j < len(w)):
91-
return False
92+
for i, w in enumerate(words):
93+
for j, c in enumerate(w):
94+
if j >= m or i >= len(words[j]) or c != words[j][i]:
95+
return False
9296
return True
9397
```
9498

@@ -171,27 +175,4 @@ function validWordSquare(words: string[]): boolean {
171175

172176
<!-- solution:end -->
173177

174-
<!-- solution:start -->
175-
176-
### Solution 2
177-
178-
<!-- tabs:start -->
179-
180-
#### Python3
181-
182-
```python
183-
class Solution:
184-
def validWordSquare(self, words: List[str]) -> bool:
185-
m = len(words)
186-
for i, w in enumerate(words):
187-
for j, c in enumerate(w):
188-
if j >= m or i >= len(words[j]) or c != words[j][i]:
189-
return False
190-
return True
191-
```
192-
193-
<!-- tabs:end -->
194-
195-
<!-- solution:end -->
196-
197178
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution:
22
def validWordSquare(self, words: List[str]) -> bool:
33
m = len(words)
4-
n = max(len(w) for w in words)
5-
if m != n:
6-
return False
7-
for j in range(n):
8-
if words[j] != "".join(w[j] for w in words if j < len(w)):
9-
return False
4+
for i, w in enumerate(words):
5+
for j, c in enumerate(w):
6+
if j >= m or i >= len(words[j]) or c != words[j][i]:
7+
return False
108
return True

solution/0400-0499/0422.Valid Word Square/Solution2.py

-8
This file was deleted.

solution/1000-1099/1023.Camelcase Matching/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ tags:
2424

2525
<p>给你一个字符串数组 <code>queries</code>,和一个表示模式的字符串&nbsp;<code>pattern</code>,请你返回一个布尔数组 <code>answer</code> 。只有在待查项&nbsp;<code>queries[i]</code> 与模式串&nbsp;<code>pattern</code> 匹配时,&nbsp;<code>answer[i]</code>&nbsp;才为 <code>true</code>,否则为 <code>false</code>。</p>
2626

27-
<p>如果可以将<strong>小写字母</strong>插入模式串&nbsp;<code>pattern</code>&nbsp;得到待查询项&nbsp;<code>query</code>,那么待查询项与给定模式串匹配。可以在任何位置插入每个字符,也可以不插入字符。</p>
27+
<p>如果可以将<strong>小写字母</strong>插入模式串&nbsp;<code>pattern</code>&nbsp;得到待查询项&nbsp;<code>queries[i]</code>,那么待查询项与给定模式串匹配。可以在任何位置插入每个字符,也可以不插入字符。</p>
2828

2929
<p>&nbsp;</p>
3030

solution/1300-1399/1325.Delete Leaves With a Given Value/README.md

+11-21
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ tags:
3030

3131
<p><strong>示例 1:</strong></p>
3232

33-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1325.Delete%20Leaves%20With%20a%20Given%20Value/images/sample_1_1684.png" style="height: 120px; width: 550px;"></strong></p>
33+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1325.Delete%20Leaves%20With%20a%20Given%20Value/images/sample_1_1684.png" style="width: 500px; height: 112px;" /></strong></p>
3434

35-
<pre><strong>输入:</strong>root = [1,2,3,2,null,2,4], target = 2
35+
<pre>
36+
<strong>输入:</strong>root = [1,2,3,2,null,2,4], target = 2
3637
<strong>输出:</strong>[1,null,3,null,4]
3738
<strong>解释:
3839
</strong>上面左边的图中,绿色节点为叶子节点,且它们的值与 target 相同(同为 2 ),它们会被删除,得到中间的图。
@@ -41,40 +42,29 @@ tags:
4142

4243
<p><strong>示例 2:</strong></p>
4344

44-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1325.Delete%20Leaves%20With%20a%20Given%20Value/images/sample_2_1684.png" style="height: 120px; width: 300px;"></strong></p>
45+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1325.Delete%20Leaves%20With%20a%20Given%20Value/images/sample_2_1684.png" style="width: 400px; height: 154px;" /></strong></p>
4546

46-
<pre><strong>输入:</strong>root = [1,3,3,3,2], target = 3
47+
<pre>
48+
<strong>输入:</strong>root = [1,3,3,3,2], target = 3
4749
<strong>输出:</strong>[1,3,null,null,2]
4850
</pre>
4951

5052
<p><strong>示例 3:</strong></p>
5153

52-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1325.Delete%20Leaves%20With%20a%20Given%20Value/images/sample_3_1684.png" style="width: 450px;"></strong></p>
54+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1325.Delete%20Leaves%20With%20a%20Given%20Value/images/sample_3_1684.png" style="width: 450px;" /></strong></p>
5355

54-
<pre><strong>输入:</strong>root = [1,2,null,2,null,2], target = 2
56+
<pre>
57+
<strong>输入:</strong>root = [1,2,null,2,null,2], target = 2
5558
<strong>输出:</strong>[1]
5659
<strong>解释:</strong>每一步都删除一个绿色的叶子节点(值为 2)。</pre>
5760

58-
<p><strong>示例 4:</strong></p>
59-
60-
<pre><strong>输入:</strong>root = [1,1,1], target = 1
61-
<strong>输出:</strong>[]
62-
</pre>
63-
64-
<p><strong>示例 5:</strong></p>
65-
66-
<pre><strong>输入:</strong>root = [1,2,3], target = 1
67-
<strong>输出:</strong>[1,2,3]
68-
</pre>
69-
7061
<p>&nbsp;</p>
7162

7263
<p><strong>提示:</strong></p>
7364

7465
<ul>
75-
<li><code>1 &lt;= target&nbsp;&lt;= 1000</code></li>
76-
<li>每一棵树最多有 <code>3000</code> 个节点。</li>
77-
<li>每一个节点值的范围是&nbsp;<code>[1, 1000]</code>&nbsp;。</li>
66+
<li>树中节点数量的范围是 <code>[1, 3000]</code>。</li>
67+
<li><code>1 &lt;= Node.val, target &lt;= 1000</code></li>
7868
</ul>
7969

8070
<!-- description:end -->

solution/2600-2699/2623.Memoize/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const memoizedSum = memoize(sum);
4242
memoizedSum (2, 2);// "call" - 返回 4。sum() 被调用,因为之前没有使用参数 (2, 2) 调用过。
4343
memoizedSum (2, 2);// "call" - 返回 4。没有调用 sum(),因为前面有相同的输入。
4444
// "getCallCount" - 总调用数: 1
45-
memoizedSum(12);// "call" - 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。
45+
memoizedSum(1, 2);// "call" - 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。
4646
// "getCallCount" - 总调用数: 2
4747
</pre>
4848

solution/2600-2699/2644.Find the Maximum Divisibility Score/README.md

+54-35
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,67 @@ tags:
1818

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

21-
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和 <code>divisors</code> 。</p>
21+
<p>给你两个整数数组 <code>nums</code> 和 <code>divisors</code> 。</p>
2222

2323
<p><code>divisors[i]</code> 的 <strong>可整除性得分</strong> 等于满足 <code>nums[j]</code> 能被 <code>divisors[i]</code> 整除的下标 <code>j</code> 的数量。</p>
2424

2525
<p>返回 <strong>可整除性得分</strong> 最大的整数 <code>divisors[i]</code> 。如果有多个整数具有最大得分,则返回数值最小的一个。</p>
2626

2727
<p>&nbsp;</p>
2828

29-
<p><strong>示例 1:</strong></p>
30-
31-
<pre>
32-
<strong>输入:</strong>nums = [4,7,9,3,9], divisors = [5,2,3]
33-
<strong>输出:</strong>3
34-
<strong>解释:</strong>divisors 中每个元素的可整除性得分为:
35-
divisors[0] 的可整除性得分为 0 ,因为 nums 中没有任何数字能被 5 整除。
36-
divisors[1] 的可整除性得分为 1 ,因为 nums[0] 能被 2 整除。
37-
divisors[2] 的可整除性得分为 3 ,因为 nums[2]、nums[3] 和 nums[4] 都能被 3 整除。
38-
因此,返回 divisors[2] ,它的可整除性得分最大。
39-
</pre>
40-
41-
<p><strong>示例 2:</strong></p>
42-
43-
<pre>
44-
<strong>输入:</strong>nums = [20,14,21,10], divisors = [5,7,5]
45-
<strong>输出:</strong>5
46-
<strong>解释:</strong>divisors 中每个元素的可整除性得分为:
47-
divisors[0] 的可整除性得分为 2 ,因为 nums[0] 和 nums[3] 都能被 5 整除。
48-
divisors[1] 的可整除性得分为 2 ,因为 nums[1] 和 nums[2] 都能被 7 整除。
49-
divisors[2] 的可整除性得分为 2 ,因为 nums[0] 和 nums[3] 都能被5整除。
50-
由于 divisors[0]、divisors[1] 和 divisors[2] 的可整除性得分都是最大的,因此,我们返回数值最小的一个,即 divisors[2] 。
51-
</pre>
52-
53-
<p><strong>示例 3:</strong></p>
54-
55-
<pre>
56-
<strong>输入:</strong>nums = [12], divisors = [10,16]
57-
<strong>输出:</strong>10
58-
<strong>解释:</strong>divisors 中每个元素的可整除性得分为:
59-
divisors[0] 的可整除性得分为 0 ,因为 nums 中没有任何数字能被 10 整除。
60-
divisors[1] 的可整除性得分为 0 ,因为 nums 中没有任何数字能被 16 整除。
61-
由于 divisors[0] 和 divisors[1] 的可整除性得分都是最大的,因此,我们返回数值最小的一个,即 divisors[0] 。
62-
</pre>
29+
<p><strong class="example">示例 1:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>输入:</strong><span class="example-io">nums = [2,9,15,50], divisors = [5,3,7,2]</span></p>
33+
34+
<p><strong>输出:</strong><span class="example-io">2</span></p>
35+
36+
<p><strong>解释:</strong></p>
37+
38+
<p><code>divisors[0]</code>&nbsp;的可整除性分数为 2 因为&nbsp;<code>nums[2]</code> 和&nbsp;<code>nums[3]</code>&nbsp;能被 5 整除。</p>
39+
40+
<p><code>divisors[1]</code> 的可整除性分数为 1 因为只有 <code>nums[1]</code>&nbsp;能被 3 整除。</p>
41+
42+
<p><code>divisors[2]</code> 的可整除性分数为 0 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 7 整除。</p>
43+
44+
<p><code>divisors[3]</code> 的可整除性分数为 2 因为 <code>nums[0]</code> 和&nbsp;<code>nums[3]</code>&nbsp;能够被 2 整除。</p>
45+
46+
<p>因为&nbsp;<code>divisors[0]</code> 和&nbsp;<code>divisors[3]</code>&nbsp;有相同的可整除性分数,我们返回更小的那个&nbsp;<code>divisors[3]</code>。</p>
47+
</div>
48+
49+
<p><strong class="example">示例 2:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>输入:</strong><span class="example-io">nums = [4,7,9,3,9], divisors = [5,2,3]</span></p>
53+
54+
<p><strong>输出:</strong><span class="example-io">3</span></p>
55+
56+
<p><strong>解释:</strong></p>
57+
58+
<p><code>divisors[0]</code> 的可整除性分数为 0&nbsp;因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 5 整除。</p>
59+
60+
<p><code>divisors[1]</code> 的可整除性分数为 1 因为只有 <code>nums[0]</code>&nbsp;能被 2 整除。</p>
61+
62+
<p><code>divisors[2]</code> 的可整除性分数为 3 因为&nbsp;<code>nums[2]</code>&nbsp;,<code>nums[3]</code>&nbsp;&nbsp;<code>nums[4]</code>&nbsp;能被 3 整除。</p>
63+
</div>
64+
65+
<p><strong class="example">示例 3:</strong></p>
66+
67+
<div class="example-block">
68+
<p><strong>输入:</strong><span class="example-io">nums = [20,14,21,10], divisors = [10,16,20]</span></p>
69+
70+
<p><strong>输出:</strong><span class="example-io">10</span></p>
71+
72+
<p><strong>解释:</strong></p>
73+
74+
<p><code>divisors[0]</code> 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 10 整除。</p>
75+
76+
<p><code>divisors[1]</code> 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 16&nbsp;整除。</p>
77+
78+
<p><code>divisors[2]</code> 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 20&nbsp;整除。</p>
79+
80+
<p>因为&nbsp;<code>divisors[0]</code>,<code>divisors[1]</code> 和&nbsp;<code>divisors[2]</code>&nbsp;都有相同的可整除性分数,我们返回最小的那个&nbsp;<code>divisors[0]</code>。</p>
81+
</div>
6382

6483
<p>&nbsp;</p>
6584

0 commit comments

Comments
 (0)