Skip to content

Commit e577217

Browse files
committed
feat: add new lc problems
1 parent 63a96ed commit e577217

File tree

46 files changed

+2073
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2073
-138
lines changed

lcof/面试题55 - I. 二叉树的深度/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Solution:
6969
return 0
7070
l, r = dfs(root.left), dfs(root.right)
7171
return 1 + max(l, r)
72-
72+
7373
return dfs(root)
7474
```
7575

solution/0000-0099/0006.Zigzag Conversion/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [6. Z 字形变换](https://leetcode.cn/problems/zigzag-conversion)
1+
# [6. N 字形变换](https://leetcode.cn/problems/zigzag-conversion)
22

33
[English Version](/solution/0000-0099/0006.Zigzag%20Conversion/README_EN.md)
44

solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md

+22-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

9-
<p>给定一个二叉树</p>
9+
<p>给定一个二叉树</p>
1010

1111
<pre>
1212
struct Node {
@@ -16,40 +16,41 @@ struct Node {
1616
Node *next;
1717
}</pre>
1818

19-
<p>填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 <code>NULL</code>。</p>
19+
<p>填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 <code>NULL</code> 。</p>
2020

21-
<p>初始状态下,所有 next 指针都被设置为 <code>NULL</code>。</p>
21+
<p>初始状态下,所有&nbsp;next 指针都被设置为 <code>NULL</code> 。</p>
2222

23-
<p> </p>
24-
25-
<p><strong>进阶:</strong></p>
26-
27-
<ul>
28-
<li>你只能使用常量级额外空间。</li>
29-
<li>使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。</li>
30-
</ul>
31-
32-
<p> </p>
33-
34-
<p><strong>示例:</strong></p>
35-
36-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0117.Populating%20Next%20Right%20Pointers%20in%20Each%20Node%20II/images/117_sample.png" style="height: 218px; width: 640px;" /></p>
23+
<p>&nbsp;</p>
3724

25+
<p><strong>示例 1:</strong></p>
26+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0117.Populating%20Next%20Right%20Pointers%20in%20Each%20Node%20II/images/117_sample.png" style="width: 500px; height: 171px;" />
3827
<pre>
3928
<strong>输入</strong>:root = [1,2,3,4,5,null,7]
4029
<strong>输出:</strong>[1,#,2,3,#,4,5,7,#]
4130
<strong>解释:</strong>给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),'#' 表示每层的末尾。</pre>
4231

43-
<p> </p>
32+
<p><strong class="example">示例 2:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong>root = []
36+
<strong>输出:</strong>[]
37+
</pre>
38+
39+
<p>&nbsp;</p>
4440

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

4743
<ul>
48-
<li>树中的节点数小于 <code>6000</code></li>
49-
<li><code>-100 <= node.val <= 100</code></li>
44+
<li>树中的节点数在范围 <code>[0, 6000]</code></li>
45+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
5046
</ul>
5147

52-
<p> </p>
48+
<p><strong>进阶:</strong></p>
49+
50+
<ul>
51+
<li>你只能使用常量级额外空间。</li>
52+
<li>使用递归解题也符合要求,本题中递归程序的隐式栈空间不计入额外空间复杂度。</li>
53+
</ul>
5354

5455
<ul>
5556
</ul>

solution/0100-0199/0189.Rotate Array/README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

9-
<p>给你一个数组,将数组中的元素向右轮转 <code>k</code><em>&nbsp;</em>个位置,其中&nbsp;<code>k</code><em>&nbsp;</em>是非负数。</p>
9+
<p>给定一个整数数组 <code>nums</code>,将数组中的元素向右轮转 <code>k</code><em>&nbsp;</em>个位置,其中&nbsp;<code>k</code><em>&nbsp;</em>是非负数。</p>
1010

1111
<p>&nbsp;</p>
1212

@@ -49,12 +49,6 @@
4949
<li>你可以使用空间复杂度为&nbsp;<code>O(1)</code> 的&nbsp;<strong>原地&nbsp;</strong>算法解决这个问题吗?</li>
5050
</ul>
5151

52-
<ul>
53-
</ul>
54-
55-
<ul>
56-
</ul>
57-
5852
## 解法
5953

6054
<!-- 这里可写通用的实现逻辑 -->

solution/0300-0399/0381.Insert Delete GetRandom O(1) - Duplicates allowed/README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@
3434

3535
<strong>解释</strong>
3636
RandomizedCollection collection = new RandomizedCollection();// 初始化一个空的集合。
37-
collection.insert(1);// 向集合中插入 1 。返回 true 表示集合不包含 1 。
38-
collection.insert(1);// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
39-
collection.insert(2);// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
40-
collection.getRandom();// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
41-
collection.remove(1);// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
42-
collection.getRandom();// getRandom 应有相同概率返回 1 和 2 。
43-
</pre>
37+
collection.insert(1); // 返回 true,因为集合不包含 1。
38+
// 将 1 插入到集合中。
39+
collection.insert(1); // 返回 false,因为集合包含 1。
40+
&nbsp; // 将另一个 1 插入到集合中。集合现在包含 [1,1]。
41+
collection.insert(2); // 返回 true,因为集合不包含 2。
42+
&nbsp; // 将 2 插入到集合中。集合现在包含 [1,1,2]。
43+
collection.getRandom(); // getRandom 应当:
44+
&nbsp; // 有 2/3 的概率返回 1,
45+
&nbsp; // 1/3 的概率返回 2。
46+
collection.remove(1); // 返回 true,因为集合包含 1。
47+
&nbsp; // 从集合中移除 1。集合现在包含 [1,2]。
48+
collection.getRandom(); // getRandom 应该返回 1 或 2,两者的可能性相同。</pre>
4449

4550
<p>&nbsp;</p>
4651

solution/0800-0899/0858.Mirror Reflection/README.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,34 @@
66

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

9-
<p>有一个特殊的正方形房间,每面墙上都有一面镜子。除西南角以外,每个角落都放有一个接受器,编号为 <code>0</code>, <code>1</code>,以及 <code>2</code>。</p>
9+
<p>有一个特殊的正方形房间,每面墙上都有一面镜子。除西南角以外,每个角落都放有一个接受器,编号为&nbsp;<code>0</code>,&nbsp;<code>1</code>,以及&nbsp;<code>2</code>。</p>
1010

11-
<p>正方形房间的墙壁长度为 <code>p</code>,一束激光从西南角射出,首先会与东墙相遇,入射点到接收器 <code>0</code> 的距离为 <code>q</code> 。</p>
11+
<p>正方形房间的墙壁长度为&nbsp;<code>p</code>,一束激光从西南角射出,首先会与东墙相遇,入射点到接收器 <code>0</code> 的距离为 <code>q</code> 。</p>
1212

1313
<p>返回光线最先遇到的接收器的编号(保证光线最终会遇到一个接收器)。</p>
14+
&nbsp;
1415

15-
<p> </p>
16+
<p><strong class="example">示例 1:</strong></p>
17+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0858.Mirror%20Reflection/images/reflection.png" style="width: 218px; height: 217px;" />
18+
<pre>
19+
<strong>输入:</strong>p = 2, q = 1
20+
<strong>输出:</strong>2
21+
<strong>解释:</strong>这条光线在第一次被反射回左边的墙时就遇到了接收器 2 。
22+
</pre>
1623

17-
<p><strong>示例:</strong></p>
24+
<p><strong class="example">示例 2:</strong></p>
1825

1926
<pre>
20-
<strong>输入: </strong>p = 2, q = 1
21-
<strong>输出: </strong>2
22-
<strong>解释: </strong>这条光线在第一次被反射回左边的墙时就遇到了接收器 2 。
23-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0858.Mirror%20Reflection/images/reflection.png" style="height: 217px; width: 218px;" /></pre>
27+
<strong>输入:</strong>p = 3, q = 1
28+
<strong>输入:</strong>1
29+
</pre>
2430

25-
<p> </p>
31+
<p>&nbsp;</p>
2632

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

2935
<ul>
30-
<li><code>1 <= p <= 1000</code></li>
31-
<li><code>0 <= q <= p</code></li>
36+
<li><code>1 &lt;= q &lt;= p &lt;= 1000</code></li>
3237
</ul>
3338

3439
## 解法

solution/1000-1099/1093.Statistics from a Large Sample/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<pre>
3333
<strong>输入:</strong>count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
3434
<strong>输出:</strong>[1.00000,3.00000,2.37500,2.50000,3.00000]
35-
<strong>解释:</strong>用count表示的样本为[1,2,2,2,3,3,3,3,3]。
35+
<strong>解释:</strong>用count表示的样本为[1,2,2,2,3,3,3,3]。
3636
最小值和最大值分别为1和3。
3737
均值是(1+2+2+2+3+3+3+3) / 8 = 19 / 8 = 2.375。
3838
因为样本的大小是偶数,所以中位数是中间两个元素2和3的平均值,也就是2.5。

solution/1100-1199/1145.Binary Tree Coloring Game/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<p>「一号」玩家给值为&nbsp;<code>x</code>&nbsp;的节点染上红色,而「二号」玩家给值为&nbsp;<code>y</code>&nbsp;的节点染上蓝色。</p>
1919

20-
<p>之后两位玩家轮流进行操作,「一号」玩家先手。每一回合,玩家选择一个之前涂好颜色的节点,将所选节点一个 <strong>未着色 </strong>的邻节点(即左右子节点、或父节点)进行染色(「一号」玩家染红色,「二号」玩家染蓝色)。</p>
20+
<p>之后两位玩家轮流进行操作,「一号」玩家先手。每一回合,玩家选择一个被他染过色的节点,将所选节点一个 <strong>未着色 </strong>的邻节点(即左右子节点、或父节点)进行染色(「一号」玩家染红色,「二号」玩家染蓝色)。</p>
2121

2222
<p>如果(且仅在此种情况下)当前玩家无法找到这样的节点来染色时,其回合就会被跳过。</p>
2323

solution/1200-1299/1209.Remove All Adjacent Duplicates in String II/README.md

+121
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,143 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:栈**
55+
56+
我们可以遍历字符串 $s$,维护一个栈,栈中存储的是字符和该字符出现的次数。当遍历到字符 $c$ 时,如果栈顶元素的字符和 $c$ 相同,则将栈顶元素的次数加一,否则将字符 $c$ 和次数 $1$ 入栈。当栈顶元素的次数等于 $k$ 时,将栈顶元素出栈。
57+
58+
遍历完字符串 $s$ 后,栈中存储的就是最终结果。我们可以将栈中的元素依次弹出,拼接成字符串即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
61+
5462
<!-- tabs:start -->
5563

5664
### **Python3**
5765

5866
<!-- 这里可写当前语言的特殊实现逻辑 -->
5967

6068
```python
69+
class Solution:
70+
def removeDuplicates(self, s: str, k: int) -> str:
71+
t = []
72+
i, n = 0, len(s)
73+
while i < n:
74+
j = i
75+
while j < n and s[j] == s[i]:
76+
j += 1
77+
cnt = j - i
78+
cnt %= k
79+
if t and t[-1][0] == s[i]:
80+
t[-1][1] = (t[-1][1] + cnt) % k
81+
if t[-1][1] == 0:
82+
t.pop()
83+
elif cnt:
84+
t.append([s[i], cnt])
85+
i = j
86+
ans = [c * v for c, v in t]
87+
return "".join(ans)
88+
```
6189

90+
```python
91+
class Solution:
92+
def removeDuplicates(self, s: str, k: int) -> str:
93+
stk = []
94+
for c in s:
95+
if stk and stk[-1][0] == c:
96+
stk[-1][1] = (stk[-1][1] + 1) % k
97+
if stk[-1][1] == 0:
98+
stk.pop()
99+
else:
100+
stk.append([c, 1])
101+
ans = [c * v for c, v in stk]
102+
return "".join(ans)
62103
```
63104

64105
### **Java**
65106

66107
<!-- 这里可写当前语言的特殊实现逻辑 -->
67108

68109
```java
110+
class Solution {
111+
public String removeDuplicates(String s, int k) {
112+
Deque<int[]> stk = new ArrayDeque<>();
113+
for (int i = 0; i < s.length(); ++i) {
114+
int j = s.charAt(i) - 'a';
115+
if (!stk.isEmpty() && stk.peek()[0] == j) {
116+
stk.peek()[1] = (stk.peek()[1] + 1) % k;
117+
if (stk.peek()[1] == 0) {
118+
stk.pop();
119+
}
120+
} else {
121+
stk.push(new int[] {j, 1});
122+
}
123+
}
124+
StringBuilder ans = new StringBuilder();
125+
for (var e : stk) {
126+
char c = (char) (e[0] + 'a');
127+
for (int i = 0; i < e[1]; ++i) {
128+
ans.append(c);
129+
}
130+
}
131+
ans.reverse();
132+
return ans.toString();
133+
}
134+
}
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
string removeDuplicates(string s, int k) {
143+
vector<pair<char, int>> stk;
144+
for (char& c : s) {
145+
if (stk.size() && stk.back().first == c) {
146+
stk.back().second = (stk.back().second + 1) % k;
147+
if (stk.back().second == 0) {
148+
stk.pop_back();
149+
}
150+
} else {
151+
stk.push_back({c, 1});
152+
}
153+
}
154+
string ans;
155+
for (auto [c, v] : stk) {
156+
ans += string(v, c);
157+
}
158+
return ans;
159+
}
160+
};
161+
```
69162
163+
### **Go**
164+
165+
```go
166+
func removeDuplicates(s string, k int) string {
167+
stk := []pair{}
168+
for _, c := range s {
169+
if len(stk) > 0 && stk[len(stk)-1].c == c {
170+
stk[len(stk)-1].v = (stk[len(stk)-1].v + 1) % k
171+
if stk[len(stk)-1].v == 0 {
172+
stk = stk[:len(stk)-1]
173+
}
174+
} else {
175+
stk = append(stk, pair{c, 1})
176+
}
177+
}
178+
ans := []rune{}
179+
for _, e := range stk {
180+
for i := 0; i < e.v; i++ {
181+
ans = append(ans, e.c)
182+
}
183+
}
184+
return string(ans)
185+
}
186+
187+
type pair struct {
188+
c rune
189+
v int
190+
}
70191
```
71192

72193
### **...**

0 commit comments

Comments
 (0)