Skip to content

Commit 93b9a06

Browse files
authored
feat: update lc problems (#2459)
1 parent 6f443df commit 93b9a06

File tree

39 files changed

+298
-236
lines changed

39 files changed

+298
-236
lines changed

solution/0000-0099/0057.Insert Interval/README.md

+14-30
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>给你一个<strong> 无重叠的</strong><em> ,</em>按照区间起始端点排序的区间列表。</p>
11+
<p>给你一个<strong> 无重叠的</strong><em> ,</em>按照区间起始端点排序的区间列表 <code>intervals</code>,其中&nbsp;<code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个区间的开始和结束,并且&nbsp;<code>intervals</code>&nbsp;按照&nbsp;<code>start<sub>i</sub></code>&nbsp;升序排列。同样给定一个区间&nbsp;<code>newInterval = [start, end]</code>&nbsp;表示另一个区间的开始和结束。</p>
1212

13-
<p>在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。</p>
13+
<p>&nbsp;<code>intervals</code> 中插入区间&nbsp;<code>newInterval</code>,使得&nbsp;<code>intervals</code>&nbsp;依然按照&nbsp;<code>start<sub>i</sub></code>&nbsp;升序排列,且区间之间不重叠(如果有必要的话,可以合并区间)。</p>
1414

15-
<p> </p>
15+
<p>返回插入之后的&nbsp;<code>intervals</code>。</p>
1616

17-
<p><strong>示例 1:</strong></p>
17+
<p><strong>注意</strong> 你不需要原地修改&nbsp;<code>intervals</code>。你可以创建一个新数组然后返回它。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>示例&nbsp;1:</strong></p>
1822

1923
<pre>
2024
<strong>输入:</strong>intervals = [[1,3],[6,9]], newInterval = [2,5]
@@ -26,40 +30,20 @@
2630
<pre>
2731
<strong>输入:</strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
2832
<strong>输出:</strong>[[1,2],[3,10],[12,16]]
29-
<strong>解释:</strong>这是因为新的区间 <code>[4,8]</code> 与 <code>[3,5],[6,7],[8,10]</code> 重叠。</pre>
30-
31-
<p><strong>示例 3:</strong></p>
32-
33-
<pre>
34-
<strong>输入:</strong>intervals = [], newInterval = [5,7]
35-
<strong>输出:</strong>[[5,7]]
36-
</pre>
37-
38-
<p><strong>示例 4:</strong></p>
39-
40-
<pre>
41-
<strong>输入:</strong>intervals = [[1,5]], newInterval = [2,3]
42-
<strong>输出:</strong>[[1,5]]
43-
</pre>
44-
45-
<p><strong>示例 5:</strong></p>
46-
47-
<pre>
48-
<strong>输入:</strong>intervals = [[1,5]], newInterval = [2,7]
49-
<strong>输出:</strong>[[1,7]]
33+
<strong>解释:</strong>这是因为新的区间 <code>[4,8]</code> 与 <code>[3,5],[6,7],[8,10]</code>&nbsp;重叠。
5034
</pre>
5135

52-
<p> </p>
36+
<p>&nbsp;</p>
5337

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

5640
<ul>
57-
<li><code>0 <= intervals.length <= 10<sup>4</sup></code></li>
41+
<li><code>0 &lt;= intervals.length &lt;= 10<sup>4</sup></code></li>
5842
<li><code>intervals[i].length == 2</code></li>
59-
<li><code>0 <= intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li>
60-
<li><code>intervals</code> 根据 <code>intervals[i][0]</code> 按 <strong>升序</strong> 排列</li>
43+
<li><code>0 &lt;=&nbsp;start<sub>i</sub> &lt;=&nbsp;end<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
44+
<li><code>intervals</code> 根据 <code>start<sub>i</sub></code> 按 <strong>升序</strong> 排列</li>
6145
<li><code>newInterval.length == 2</code></li>
62-
<li><code>0 <= newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>
46+
<li><code>0 &lt;=&nbsp;start &lt;=&nbsp;end &lt;= 10<sup>5</sup></code></li>
6347
</ul>
6448

6549
## 解法

solution/0000-0099/0057.Insert Interval/README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<p>Return <code>intervals</code><em> after the insertion</em>.</p>
1414

15+
<p><strong>Note</strong> that you don&#39;t need to modify <code>intervals</code> in-place. You can make a new array and return it.</p>
16+
1517
<p>&nbsp;</p>
1618
<p><strong class="example">Example 1:</strong></p>
1719

solution/0000-0099/0090.Subsets II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的&lt;span data-keyword="subset"&gt;子集&lt;/span&gt;(幂集)。</p>
11+
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的 <span data-keyword="subset">子集</span>(幂集)。</p>
1212

1313
<p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p>
1414

solution/0300-0399/0310.Minimum Height Trees/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。</p>
11+
<p>树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,任何一个没有简单环路的连通图都是一棵树。</p>
1212

1313
<p>给你一棵包含&nbsp;<code>n</code>&nbsp;个节点的树,标记为&nbsp;<code>0</code>&nbsp;&nbsp;<code>n - 1</code> 。给定数字&nbsp;<code>n</code>&nbsp;和一个有 <code>n - 1</code> 条无向边的 <code>edges</code>&nbsp;列表(每一个边都是一对标签),其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示树中节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间存在一条无向边。</p>
1414

solution/0700-0799/0791.Custom Sort String/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<p>Return <em>any permutation of </em><code>s</code><em> that satisfies this property</em>.</p>
1414

1515
<p>&nbsp;</p>
16-
<p><strong class="example">Example 1: </strong></p>
16+
<p><strong class="example">Example 1:</strong></p>
1717

1818
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
1919
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> order = &quot;cba&quot;, s = &quot;abcd&quot; </span></p>
@@ -25,7 +25,7 @@
2525
<p>Since <code>&quot;d&quot;</code> does not appear in <code>order</code>, it can be at any position in the returned string. <code>&quot;dcba&quot;</code>, <code>&quot;cdba&quot;</code>, <code>&quot;cbda&quot;</code> are also valid outputs.</p>
2626
</div>
2727

28-
<p><strong class="example">Example 2: </strong></p>
28+
<p><strong class="example">Example 2:</strong></p>
2929

3030
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
3131
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> order = &quot;bcafg&quot;, s = &quot;abcd&quot; </span></p>

solution/1000-1099/1018.Binary Prefix Divisible By 5/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/1000-1099/1018.Binary%20Prefix%20Divisible%20By%205/README_EN.md)
44

5-
<!-- tags:数组 -->
5+
<!-- tags:位运算,数组 -->
66

77
## 题目描述
88

solution/1000-1099/1018.Binary Prefix Divisible By 5/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/1000-1099/1018.Binary%20Prefix%20Divisible%20By%205/README.md)
44

5-
<!-- tags:Array -->
5+
<!-- tags:Bit Manipulation,Array -->
66

77
## Description
88

solution/1000-1099/1035.Uncrossed Lines/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<p>在两条独立的水平线上按给定的顺序写下 <code>nums1</code> 和 <code>nums2</code> 中的整数。</p>
1212

13-
<p>现在,可以绘制一些连接两个数字 <code>nums1[i]</code>&nbsp;和 <code>nums2[j]</code>&nbsp;的直线,这些直线需要同时满足满足:</p>
13+
<p>现在,可以绘制一些连接两个数字 <code>nums1[i]</code>&nbsp;和 <code>nums2[j]</code>&nbsp;的直线,这些直线需要同时满足:</p>
1414

1515
<ul>
1616
<li>&nbsp;<code>nums1[i] == nums2[j]</code></li>

solution/1100-1199/1151.Minimum Swaps to Group All 1's Together/README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@
6464

6565
我们先统计数组中 $1$ 的个数,记为 $k$。然后我们使用滑动窗口,窗口大小为 $k$,窗口右边界从左向右移动,统计窗口内 $1$ 的个数,记为 $t$。每次移动窗口时,都更新 $t$ 的值,最后窗口右边界移动到数组末尾时,窗口内 $1$ 的个数最多,记为 $mx$。最后答案为 $k - mx$。
6666

67-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
67+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
6868

6969
<!-- tabs:start -->
7070

7171
```python
7272
class Solution:
7373
def minSwaps(self, data: List[int]) -> int:
7474
k = data.count(1)
75-
t = sum(data[:k])
76-
mx = t
75+
mx = t = sum(data[:k])
7776
for i in range(k, len(data)):
7877
t += data[i]
7978
t -= data[i - k]
@@ -159,6 +158,22 @@ function minSwaps(data: number[]): number {
159158
}
160159
```
161160

161+
```cs
162+
public class Solution {
163+
public int MinSwaps(int[] data) {
164+
int k = data.Count(x => x == 1);
165+
int t = data.Take(k).Sum();
166+
int mx = t;
167+
for (int i = k; i < data.Length; ++i) {
168+
t += data[i];
169+
t -= data[i - k];
170+
mx = Math.Max(mx, t);
171+
}
172+
return k - mx;
173+
}
174+
}
175+
```
176+
162177
<!-- tabs:end -->
163178

164179
<!-- end -->

solution/1100-1199/1151.Minimum Swaps to Group All 1's Together/README_EN.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is
5959
class Solution:
6060
def minSwaps(self, data: List[int]) -> int:
6161
k = data.count(1)
62-
t = sum(data[:k])
63-
mx = t
62+
mx = t = sum(data[:k])
6463
for i in range(k, len(data)):
6564
t += data[i]
6665
t -= data[i - k]
@@ -146,6 +145,22 @@ function minSwaps(data: number[]): number {
146145
}
147146
```
148147

148+
```cs
149+
public class Solution {
150+
public int MinSwaps(int[] data) {
151+
int k = data.Count(x => x == 1);
152+
int t = data.Take(k).Sum();
153+
int mx = t;
154+
for (int i = k; i < data.Length; ++i) {
155+
t += data[i];
156+
t -= data[i - k];
157+
mx = Math.Max(mx, t);
158+
}
159+
return k - mx;
160+
}
161+
}
162+
```
163+
149164
<!-- tabs:end -->
150165

151166
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int MinSwaps(int[] data) {
3+
int k = data.Count(x => x == 1);
4+
int t = data.Take(k).Sum();
5+
int mx = t;
6+
for (int i = k; i < data.Length; ++i) {
7+
t += data[i];
8+
t -= data[i - k];
9+
mx = Math.Max(mx, t);
10+
}
11+
return k - mx;
12+
}
13+
}

solution/1100-1199/1151.Minimum Swaps to Group All 1's Together/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def minSwaps(self, data: List[int]) -> int:
33
k = data.count(1)
4-
t = sum(data[:k])
5-
mx = t
4+
mx = t = sum(data[:k])
65
for i in range(k, len(data)):
76
t += data[i]
87
t -= data[i - k]

solution/1300-1399/1361.Validate Binary Tree Nodes/README.md

+11-16
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,28 @@
2020

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

23-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex1.png" style="height: 287px; width: 195px;"></strong></p>
23+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex1.png" style="height: 287px; width: 195px;" /></strong></p>
2424

25-
<pre><strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
25+
<pre>
26+
<strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
2627
<strong>输出:</strong>true
2728
</pre>
2829

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

31-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex2.png" style="height: 272px; width: 183px;"></strong></p>
32+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex2.png" style="height: 272px; width: 183px;" /></strong></p>
3233

33-
<pre><strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
34+
<pre>
35+
<strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
3436
<strong>输出:</strong>false
3537
</pre>
3638

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

39-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex3.png" style="height: 174px; width: 82px;"></strong></p>
41+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex3.png" style="height: 174px; width: 82px;" /></strong></p>
4042

41-
<pre><strong>输入:</strong>n = 2, leftChild = [1,0], rightChild = [-1,-1]
42-
<strong>输出:</strong>false
43-
</pre>
44-
45-
<p><strong>示例 4:</strong></p>
46-
47-
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1361.Validate%20Binary%20Tree%20Nodes/images/1503_ex4.png" style="height: 191px; width: 470px;"></strong></p>
48-
49-
<pre><strong>输入:</strong>n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1]
43+
<pre>
44+
<strong>输入:</strong>n = 2, leftChild = [1,0], rightChild = [-1,-1]
5045
<strong>输出:</strong>false
5146
</pre>
5247

@@ -55,8 +50,8 @@
5550
<p><strong>提示:</strong></p>
5651

5752
<ul>
58-
<li><code>1 &lt;= n &lt;= 10^4</code></li>
59-
<li><code>leftChild.length == rightChild.length == n</code></li>
53+
<li><code>n == leftChild.length == rightChild.length</code></li>
54+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
6055
<li><code>-1 &lt;= leftChild[i], rightChild[i] &lt;= n - 1</code></li>
6156
</ul>
6257

solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<p>Given two integer arrays <code>arr1</code> and <code>arr2</code>, and the integer <code>d</code>, <em>return the distance value between the two arrays</em>.</p>
1010

11-
<p>The distance value is defined as the number of elements <code>arr1[i]</code> such that there is not any element <code>arr2[j]</code> where <code>|arr1[i]-arr2[j]| &lt;= d</code>.</p>
11+
<p>The distance value is defined as the number of elements <code>arr1[i]</code> such that there is not any element <code>arr2[j]</code> where <code>|arr1[i]-arr2[j]| &lt; d</code>.</p>
1212

1313
<p>&nbsp;</p>
1414
<p><strong class="example">Example 1:</strong></p>

0 commit comments

Comments
 (0)