Skip to content

feat: update lc problems #3023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ class Solution {
var i = i
var j = j
let chars = Array(s)

while i >= 0 && j < chars.count && chars[i] == chars[j] {
cnt += 1
i -= 1
j += 1
}

return cnt
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,18 @@ class Solution {
let dummy = ListNode(0, head)
var fast: ListNode? = dummy
var slow: ListNode? = dummy

var n = n
while n > 0 {
fast = fast?.next
n -= 1
}

while fast?.next != nil {
slow = slow?.next
fast = fast?.next
}

slow?.next = slow?.next?.next
return dummy.next
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ class Solution {
func detectCycle(_ head: ListNode?) -> ListNode? {
var fast = head
var slow = head

while fast != nil && fast?.next != nil {
slow = slow?.next
fast = fast?.next?.next

if slow === fast {
var ans = head
while ans !== slow {
Expand Down
1 change: 1 addition & 0 deletions solution/0300-0399/0324.Wiggle Sort II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0324.Wiggle%20Sort%20II/README.md
tags:
- 贪心
- 数组
- 分治
- 快速选择
Expand Down
1 change: 1 addition & 0 deletions solution/0300-0399/0324.Wiggle Sort II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0300-0399/0324.Wiggle%20Sort%20II/README_EN.md
tags:
- Greedy
- Array
- Divide and Conquer
- Quickselect
Expand Down
1 change: 1 addition & 0 deletions solution/0300-0399/0399.Evaluate Division/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags:
- 并查集
- 图
- 数组
- 字符串
- 最短路
---

Expand Down
1 change: 1 addition & 0 deletions solution/0300-0399/0399.Evaluate Division/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags:
- Union Find
- Graph
- Array
- String
- Shortest Path
---

Expand Down
33 changes: 4 additions & 29 deletions solution/0400-0499/0422.Valid Word Square/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ tags:
class Solution:
def validWordSquare(self, words: List[str]) -> bool:
m = len(words)
n = max(len(w) for w in words)
if m != n:
return False
for j in range(n):
if words[j] != "".join(w[j] for w in words if j < len(w)):
return False
for i, w in enumerate(words):
for j, c in enumerate(w):
if j >= m or i >= len(words[j]) or c != words[j][i]:
return False
return True
```

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

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def validWordSquare(self, words: List[str]) -> bool:
m = len(words)
for i, w in enumerate(words):
for j, c in enumerate(w):
if j >= m or i >= len(words[j]) or c != words[j][i]:
return False
return True
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
41 changes: 11 additions & 30 deletions solution/0400-0499/0422.Valid Word Square/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ Therefore, it is NOT a valid word square.

<!-- solution:start -->

### Solution 1
### Solution 1: Iterative Check

We observe that if $words[i][j] \neq words[j][i]$, we can directly return `false`.

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`.

The time complexity is $O(n^2)$, where $n$ is the length of `words`. The space complexity is $O(1)`.

<!-- tabs:start -->

Expand All @@ -83,12 +89,10 @@ Therefore, it is NOT a valid word square.
class Solution:
def validWordSquare(self, words: List[str]) -> bool:
m = len(words)
n = max(len(w) for w in words)
if m != n:
return False
for j in range(n):
if words[j] != "".join(w[j] for w in words if j < len(w)):
return False
for i, w in enumerate(words):
for j, c in enumerate(w):
if j >= m or i >= len(words[j]) or c != words[j][i]:
return False
return True
```

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

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def validWordSquare(self, words: List[str]) -> bool:
m = len(words)
for i, w in enumerate(words):
for j, c in enumerate(w):
if j >= m or i >= len(words[j]) or c != words[j][i]:
return False
return True
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
10 changes: 4 additions & 6 deletions solution/0400-0499/0422.Valid Word Square/Solution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class Solution:
def validWordSquare(self, words: List[str]) -> bool:
m = len(words)
n = max(len(w) for w in words)
if m != n:
return False
for j in range(n):
if words[j] != "".join(w[j] for w in words if j < len(w)):
return False
for i, w in enumerate(words):
for j, c in enumerate(w):
if j >= m or i >= len(words[j]) or c != words[j][i]:
return False
return True
8 changes: 0 additions & 8 deletions solution/0400-0499/0422.Valid Word Square/Solution2.py

This file was deleted.

2 changes: 1 addition & 1 deletion solution/1000-1099/1023.Camelcase Matching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tags:

<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>

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

<p>&nbsp;</p>

Expand Down
32 changes: 11 additions & 21 deletions solution/1300-1399/1325.Delete Leaves With a Given Value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ tags:

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

<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>
<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>

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

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

<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>
<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>

<pre><strong>输入:</strong>root = [1,3,3,3,2], target = 3
<pre>
<strong>输入:</strong>root = [1,3,3,3,2], target = 3
<strong>输出:</strong>[1,3,null,null,2]
</pre>

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

<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>
<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>

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

<p><strong>示例 4:</strong></p>

<pre><strong>输入:</strong>root = [1,1,1], target = 1
<strong>输出:</strong>[]
</pre>

<p><strong>示例 5:</strong></p>

<pre><strong>输入:</strong>root = [1,2,3], target = 1
<strong>输出:</strong>[1,2,3]
</pre>

<p>&nbsp;</p>

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

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

<!-- description:end -->
Expand Down
2 changes: 1 addition & 1 deletion solution/2600-2699/2623.Memoize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const memoizedSum = memoize(sum);
memoizedSum (2, 2);// "call" - 返回 4。sum() 被调用,因为之前没有使用参数 (2, 2) 调用过。
memoizedSum (2, 2);// "call" - 返回 4。没有调用 sum(),因为前面有相同的输入。
// "getCallCount" - 总调用数: 1
memoizedSum(12);// "call" - 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。
memoizedSum(1, 2);// "call" - 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。
// "getCallCount" - 总调用数: 2
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,67 @@ tags:

<!-- description:start -->

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

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

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

<p>&nbsp;</p>

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

<pre>
<strong>输入:</strong>nums = [4,7,9,3,9], divisors = [5,2,3]
<strong>输出:</strong>3
<strong>解释:</strong>divisors 中每个元素的可整除性得分为:
divisors[0] 的可整除性得分为 0 ,因为 nums 中没有任何数字能被 5 整除。
divisors[1] 的可整除性得分为 1 ,因为 nums[0] 能被 2 整除。
divisors[2] 的可整除性得分为 3 ,因为 nums[2]、nums[3] 和 nums[4] 都能被 3 整除。
因此,返回 divisors[2] ,它的可整除性得分最大。
</pre>

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

<pre>
<strong>输入:</strong>nums = [20,14,21,10], divisors = [5,7,5]
<strong>输出:</strong>5
<strong>解释:</strong>divisors 中每个元素的可整除性得分为:
divisors[0] 的可整除性得分为 2 ,因为 nums[0] 和 nums[3] 都能被 5 整除。
divisors[1] 的可整除性得分为 2 ,因为 nums[1] 和 nums[2] 都能被 7 整除。
divisors[2] 的可整除性得分为 2 ,因为 nums[0] 和 nums[3] 都能被5整除。
由于 divisors[0]、divisors[1] 和 divisors[2] 的可整除性得分都是最大的,因此,我们返回数值最小的一个,即 divisors[2] 。
</pre>

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

<pre>
<strong>输入:</strong>nums = [12], divisors = [10,16]
<strong>输出:</strong>10
<strong>解释:</strong>divisors 中每个元素的可整除性得分为:
divisors[0] 的可整除性得分为 0 ,因为 nums 中没有任何数字能被 10 整除。
divisors[1] 的可整除性得分为 0 ,因为 nums 中没有任何数字能被 16 整除。
由于 divisors[0] 和 divisors[1] 的可整除性得分都是最大的,因此,我们返回数值最小的一个,即 divisors[0] 。
</pre>
<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [2,9,15,50], divisors = [5,3,7,2]</span></p>

<p><strong>输出:</strong><span class="example-io">2</span></p>

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

<p><code>divisors[0]</code>&nbsp;的可整除性分数为 2 因为&nbsp;<code>nums[2]</code> 和&nbsp;<code>nums[3]</code>&nbsp;能被 5 整除。</p>

<p><code>divisors[1]</code> 的可整除性分数为 1 因为只有 <code>nums[1]</code>&nbsp;能被 3 整除。</p>

<p><code>divisors[2]</code> 的可整除性分数为 0 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 7 整除。</p>

<p><code>divisors[3]</code> 的可整除性分数为 2 因为 <code>nums[0]</code> 和&nbsp;<code>nums[3]</code>&nbsp;能够被 2 整除。</p>

<p>因为&nbsp;<code>divisors[0]</code> 和&nbsp;<code>divisors[3]</code>&nbsp;有相同的可整除性分数,我们返回更小的那个&nbsp;<code>divisors[3]</code>。</p>
</div>

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

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [4,7,9,3,9], divisors = [5,2,3]</span></p>

<p><strong>输出:</strong><span class="example-io">3</span></p>

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

<p><code>divisors[0]</code> 的可整除性分数为 0&nbsp;因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 5 整除。</p>

<p><code>divisors[1]</code> 的可整除性分数为 1 因为只有 <code>nums[0]</code>&nbsp;能被 2 整除。</p>

<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>
</div>

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

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [20,14,21,10], divisors = [10,16,20]</span></p>

<p><strong>输出:</strong><span class="example-io">10</span></p>

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

<p><code>divisors[0]</code> 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 10 整除。</p>

<p><code>divisors[1]</code> 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 16&nbsp;整除。</p>

<p><code>divisors[2]</code> 因为&nbsp;<code>nums</code>&nbsp;中没有数字能被 20&nbsp;整除。</p>

<p>因为&nbsp;<code>divisors[0]</code>,<code>divisors[1]</code> 和&nbsp;<code>divisors[2]</code>&nbsp;都有相同的可整除性分数,我们返回最小的那个&nbsp;<code>divisors[0]</code>。</p>
</div>

<p>&nbsp;</p>

Expand Down
Loading
Loading