Skip to content

Commit 896dc29

Browse files
committed
feat: update lc problems
1 parent 0c17c73 commit 896dc29

File tree

41 files changed

+1524
-220
lines changed

Some content is hidden

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

41 files changed

+1524
-220
lines changed

solution/0000-0099/0050.Pow(x, n)/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<li><code>-100.0 &lt; x &lt; 100.0</code></li>
3737
<li><code>-2<sup>31</sup> &lt;= n &lt;= 2<sup>31</sup>-1</code></li>
3838
<li><code>n</code> is an integer.</li>
39+
<li>Either <code>x</code> is not zero or <code>n &gt; 0</code>.</li>
3940
<li><code>-10<sup>4</sup> &lt;= x<sup>n</sup> &lt;= 10<sup>4</sup></code></li>
4041
</ul>
4142

solution/0800-0899/0802.Find Eventual Safe States/README.md

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

99
<p>有一个有 <code>n</code> 个节点的有向图,节点按 <code>0</code> 到 <code>n - 1</code> 编号。图由一个 <strong>索引从 0 开始</strong> 的 2D 整数数组&nbsp;<code>graph</code>表示,&nbsp;<code>graph[i]</code>是与节点 <code>i</code> 相邻的节点的整数数组,这意味着从节点 <code>i</code> 到&nbsp;<code>graph[i]</code>中的每个节点都有一条边。</p>
1010

11-
<p>如果一个节点没有连出的有向边,则它是 <strong>终端节点</strong> 。如果没有出边,则节点为终端节点。如果从该节点开始的所有可能路径都通向 <strong>终端节点</strong> ,则该节点为 <strong>安全节点</strong> 。</p>
11+
<p>如果一个节点没有连出的有向边,则该节点是 <strong>终端节点</strong> 。如果从该节点开始的所有可能路径都通向 <strong>终端节点</strong> ,则该节点为 <strong>安全节点</strong> 。</p>
1212

1313
<p>返回一个由图中所有 <strong>安全节点</strong> 组成的数组作为答案。答案数组中的元素应当按 <strong>升序</strong> 排列。</p>
1414

solution/1000-1099/1015.Smallest Integer Divisible by K/README.md

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

1111
<p>返回 <code>n</code>&nbsp;的长度。如果不存在这样的 <code>n</code>&nbsp;,就返回-1。</p>
1212

13-
<p><strong>注意:</strong> <code>n</code>&nbsp;不符合 64 位带符号整数。</p>
13+
<p><strong>注意:</strong> <code>n</code> 可能不符合 64 位带符号整数。</p>
1414

1515
<p>&nbsp;</p>
1616

solution/1000-1099/1072.Flip Columns For Maximum Number of Equal Rows/README.md

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

1111
<p>你可以从中选出任意数量的列并翻转其上的&nbsp;<strong>每个&nbsp;</strong>单元格。(即翻转后,单元格的值从 <code>0</code> 变成 <code>1</code>,或者从 <code>1</code> 变为 <code>0</code> 。)</p>
1212

13-
<p>返回 <em>经过一些翻转后,行与行之间所有值都相等的最大行数</em>&nbsp;。</p>
13+
<p>返回 <em>经过一些翻转后,行内所有值都相等的最大行数</em>&nbsp;。</p>
1414

1515
<p>&nbsp;</p>
1616

solution/1000-1099/1081.Smallest Subsequence of Distinct Characters/README.md

+30-49
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040

4141
**方法一:栈**
4242

43-
我们用一个数组 `last` 记录每个字符最后一次出现的位置,用栈来保存结果字符串,用一个数组 `vis` 或者一个整型变量 `mask` 记录当前字符是否在栈中。
43+
我们用一个数组 $last$ 记录字符串 $s$ 每个字符最后一次出现的位置,用栈来保存结果字符串,用一个数组 $vis$ 或者一个整型变量 $mask$ 记录当前字符是否在栈中。
4444

4545
遍历字符串 $s$,对于每个字符 $c$,如果 $c$ 不在栈中,我们就需要判断栈顶元素是否大于 $c$,如果大于 $c$,且栈顶元素在后面还会出现,我们就将栈顶元素弹出,将 $c$ 压入栈中。
4646

4747
最后将栈中元素拼接成字符串作为结果返回。
4848

49-
时间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
49+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
5050

5151
<!-- tabs:start -->
5252

@@ -57,9 +57,7 @@
5757
```python
5858
class Solution:
5959
def smallestSubsequence(self, s: str) -> str:
60-
last = defaultdict(int)
61-
for i, c in enumerate(s):
62-
last[c] = i
60+
last = {c: i for i, c in enumerate(s)}
6361
stk = []
6462
vis = set()
6563
for i, c in enumerate(s):
@@ -69,30 +67,7 @@ class Solution:
6967
vis.remove(stk.pop())
7068
stk.append(c)
7169
vis.add(c)
72-
return ''.join(stk)
73-
```
74-
75-
```python
76-
class Solution:
77-
def smallestSubsequence(self, s: str) -> str:
78-
count, in_stack = [0] * 128, [False] * 128
79-
stack = []
80-
for c in s:
81-
count[ord(c)] += 1
82-
83-
for c in s:
84-
count[ord(c)] -= 1
85-
if in_stack[ord(c)]:
86-
continue
87-
while len(stack) and stack[-1] > c:
88-
peek = stack[-1]
89-
if count[ord(peek)] < 1:
90-
break
91-
in_stack[ord(peek)] = False
92-
stack.pop()
93-
stack.append(c)
94-
in_stack[ord(c)] = True
95-
return ''.join(stack)
70+
return "".join(stk)
9671
```
9772

9873
### **Java**
@@ -209,27 +184,33 @@ func smallestSubsequence(s string) string {
209184
}
210185
```
211186

212-
```go
213-
func smallestSubsequence(s string) string {
214-
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
215-
for _, c := range s {
216-
count[c] += 1
217-
}
187+
### **TypeScript**
218188

219-
for _, c := range s {
220-
count[c] -= 1
221-
if in_stack[c] {
222-
continue
223-
}
224-
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
225-
peek := stack[len(stack)-1]
226-
stack = stack[0 : len(stack)-1]
227-
in_stack[peek] = false
228-
}
229-
stack = append(stack, c)
230-
in_stack[c] = true
231-
}
232-
return string(stack)
189+
```ts
190+
function smallestSubsequence(s: string): string {
191+
const f = (c: string): number => c.charCodeAt(0) - 'a'.charCodeAt(0);
192+
const last: number[] = new Array(26).fill(0);
193+
for (const [i, c] of [...s].entries()) {
194+
last[f(c)] = i;
195+
}
196+
const stk: string[] = [];
197+
let mask = 0;
198+
for (const [i, c] of [...s].entries()) {
199+
const x = f(c);
200+
if ((mask >> x) & 1) {
201+
continue;
202+
}
203+
while (
204+
stk.length &&
205+
stk[stk.length - 1] > c &&
206+
last[f(stk[stk.length - 1])] > i
207+
) {
208+
mask ^= 1 << f(stk.pop()!);
209+
}
210+
stk.push(c);
211+
mask |= 1 << x;
212+
}
213+
return stk.join('');
233214
}
234215
```
235216

solution/1000-1099/1081.Smallest Subsequence of Distinct Characters/README_EN.md

+28-47
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
```python
4444
class Solution:
4545
def smallestSubsequence(self, s: str) -> str:
46-
last = defaultdict(int)
47-
for i, c in enumerate(s):
48-
last[c] = i
46+
last = {c: i for i, c in enumerate(s)}
4947
stk = []
5048
vis = set()
5149
for i, c in enumerate(s):
@@ -55,30 +53,7 @@ class Solution:
5553
vis.remove(stk.pop())
5654
stk.append(c)
5755
vis.add(c)
58-
return ''.join(stk)
59-
```
60-
61-
```python
62-
class Solution:
63-
def smallestSubsequence(self, s: str) -> str:
64-
count, in_stack = [0] * 128, [False] * 128
65-
stack = []
66-
for c in s:
67-
count[ord(c)] += 1
68-
69-
for c in s:
70-
count[ord(c)] -= 1
71-
if in_stack[ord(c)]:
72-
continue
73-
while len(stack) and stack[-1] > c:
74-
peek = stack[-1]
75-
if count[ord(peek)] < 1:
76-
break
77-
in_stack[ord(peek)] = False
78-
stack.pop()
79-
stack.append(c)
80-
in_stack[ord(c)] = True
81-
return ''.join(stack)
56+
return "".join(stk)
8257
```
8358

8459
### **Java**
@@ -193,27 +168,33 @@ func smallestSubsequence(s string) string {
193168
}
194169
```
195170

196-
```go
197-
func smallestSubsequence(s string) string {
198-
count, in_stack, stack := make([]int, 128), make([]bool, 128), make([]rune, 0)
199-
for _, c := range s {
200-
count[c] += 1
201-
}
171+
### **TypeScript**
202172

203-
for _, c := range s {
204-
count[c] -= 1
205-
if in_stack[c] {
206-
continue
207-
}
208-
for len(stack) > 0 && stack[len(stack)-1] > c && count[stack[len(stack)-1]] > 0 {
209-
peek := stack[len(stack)-1]
210-
stack = stack[0 : len(stack)-1]
211-
in_stack[peek] = false
212-
}
213-
stack = append(stack, c)
214-
in_stack[c] = true
215-
}
216-
return string(stack)
173+
```ts
174+
function smallestSubsequence(s: string): string {
175+
const f = (c: string): number => c.charCodeAt(0) - 'a'.charCodeAt(0);
176+
const last: number[] = new Array(26).fill(0);
177+
for (const [i, c] of [...s].entries()) {
178+
last[f(c)] = i;
179+
}
180+
const stk: string[] = [];
181+
let mask = 0;
182+
for (const [i, c] of [...s].entries()) {
183+
const x = f(c);
184+
if ((mask >> x) & 1) {
185+
continue;
186+
}
187+
while (
188+
stk.length &&
189+
stk[stk.length - 1] > c &&
190+
last[f(stk[stk.length - 1])] > i
191+
) {
192+
mask ^= 1 << f(stk.pop()!);
193+
}
194+
stk.push(c);
195+
mask |= 1 << x;
196+
}
197+
return stk.join('');
217198
}
218199
```
219200

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Solution:
22
def smallestSubsequence(self, s: str) -> str:
3-
last = defaultdict(int)
4-
for i, c in enumerate(s):
5-
last[c] = i
3+
last = {c: i for i, c in enumerate(s)}
64
stk = []
75
vis = set()
86
for i, c in enumerate(s):
@@ -12,4 +10,4 @@ def smallestSubsequence(self, s: str) -> str:
1210
vis.remove(stk.pop())
1311
stk.append(c)
1412
vis.add(c)
15-
return ''.join(stk)
13+
return "".join(stk)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function smallestSubsequence(s: string): string {
2+
const f = (c: string): number => c.charCodeAt(0) - 'a'.charCodeAt(0);
3+
const last: number[] = new Array(26).fill(0);
4+
for (const [i, c] of [...s].entries()) {
5+
last[f(c)] = i;
6+
}
7+
const stk: string[] = [];
8+
let mask = 0;
9+
for (const [i, c] of [...s].entries()) {
10+
const x = f(c);
11+
if ((mask >> x) & 1) {
12+
continue;
13+
}
14+
while (
15+
stk.length &&
16+
stk[stk.length - 1] > c &&
17+
last[f(stk[stk.length - 1])] > i
18+
) {
19+
mask ^= 1 << f(stk.pop()!);
20+
}
21+
stk.push(c);
22+
mask |= 1 << x;
23+
}
24+
return stk.join('');
25+
}

solution/1000-1099/1082.Sales Analysis I/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ Product 表:
8787
### **SQL**
8888

8989
```sql
90-
90+
# Write your MySQL query statement below
91+
SELECT seller_id
92+
FROM Sales
93+
GROUP BY seller_id
94+
HAVING SUM(price) >= ALL (
95+
SELECT SUM(price)
96+
FROM Sales
97+
GROUP BY seller_id )
9198
```
9299

93100
<!-- tabs:end -->

solution/1000-1099/1082.Sales Analysis I/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ Sales table:
8383
### **SQL**
8484

8585
```sql
86-
86+
# Write your MySQL query statement below
87+
SELECT seller_id
88+
FROM Sales
89+
GROUP BY seller_id
90+
HAVING SUM(price) >= ALL (
91+
SELECT SUM(price)
92+
FROM Sales
93+
GROUP BY seller_id )
8794
```
8895

8996
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT seller_id
3+
FROM Sales
4+
GROUP BY seller_id
5+
HAVING SUM(price) >= ALL (
6+
SELECT SUM(price)
7+
FROM Sales
8+
GROUP BY seller_id )

solution/1000-1099/1083.Sales Analysis II/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,19 @@ id 为 1 的买家购买了一部 S8,但是却没有购买 iPhone,而 id 为
7676
### **SQL**
7777

7878
```sql
79-
79+
# Write your MySQL query statement below
80+
SELECT buyer_id
81+
FROM
82+
(
83+
SELECT buyer_id
84+
,CASE WHEN p.product_name = 'S8' THEN 1 ELSE 0 END AS s8
85+
,CASE WHEN p.product_name = 'iPhone' THEN 1 ELSE 0 END AS iPhone
86+
FROM Product p
87+
JOIN Sales s
88+
ON p.product_id = s.product_id
89+
) t
90+
GROUP BY buyer_id
91+
HAVING SUM(S8) > 0 AND SUM(iPhone) = 0;
8092
```
8193

8294
<!-- tabs:end -->

solution/1000-1099/1083.Sales Analysis II/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,19 @@ Sales table:
8484
### **SQL**
8585

8686
```sql
87-
87+
# Write your MySQL query statement below
88+
SELECT buyer_id
89+
FROM
90+
(
91+
SELECT buyer_id
92+
,CASE WHEN p.product_name = 'S8' THEN 1 ELSE 0 END AS s8
93+
,CASE WHEN p.product_name = 'iPhone' THEN 1 ELSE 0 END AS iPhone
94+
FROM Product p
95+
JOIN Sales s
96+
ON p.product_id = s.product_id
97+
) t
98+
GROUP BY buyer_id
99+
HAVING SUM(S8) > 0 AND SUM(iPhone) = 0;
88100
```
89101

90102
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Write your MySQL query statement below
2+
SELECT buyer_id
3+
FROM
4+
(
5+
SELECT buyer_id
6+
,CASE WHEN p.product_name = 'S8' THEN 1 ELSE 0 END AS s8
7+
,CASE WHEN p.product_name = 'iPhone' THEN 1 ELSE 0 END AS iPhone
8+
FROM Product p
9+
JOIN Sales s
10+
ON p.product_id = s.product_id
11+
) t
12+
GROUP BY buyer_id
13+
HAVING SUM(S8) > 0 AND SUM(iPhone) = 0;

0 commit comments

Comments
 (0)