Skip to content

Commit fbd3df3

Browse files
authored
feat: add solutions to lc problems: No.3174,3175,3182 (doocs#3086)
1 parent 3a9f57c commit fbd3df3

File tree

19 files changed

+529
-21
lines changed

19 files changed

+529
-21
lines changed

solution/1500-1599/1535.Find the Winner of an Array Game/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ tags:
8080

8181
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
8282

83+
相似题目:
84+
85+
- [3175. 找到连续赢 K 场比赛的第一位玩家](https://github.com/doocs/leetcode/blob/main/solution/3100-3199/3175.Find%20The%20First%20Player%20to%20win%20K%20Games%20in%20a%20Row/README.md)
86+
8387
<!-- tabs:start -->
8488

8589
#### Python3

solution/1500-1599/1535.Find the Winner of an Array Game/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ So we can see that 4 rounds will be played and 5 is the winner because it wins 2
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Quick Thinking
70+
71+
We notice that each time the first two elements of the array are compared, regardless of the result, the next comparison will always be between the next element in the array and the current winner. Therefore, if we have looped $n-1$ times, the final winner must be the maximum element in the array. Otherwise, if an element has won consecutively $k$ times, then this element is the final winner.
72+
73+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
74+
75+
Similar problems:
76+
77+
- [1535. Find the Winner of an Array Game](https://github.com/doocs/leetcode/blob/main/solution/3100-3199/3175.Find%20The%20First%20Player%20to%20win%20K%20Games%20in%20a%20Row/README_EN.md)
7078

7179
<!-- tabs:start -->
7280

solution/3100-3199/3174.Clear Digits/README.md

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,97 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Cl
6868

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

71-
### 方法一
71+
### 方法一:栈 + 模拟
72+
73+
我们用一个栈 `stk` 来模拟这个过程,遍历字符串 `s`,如果当前字符是数字,就弹出栈顶元素,否则将当前字符入栈。
74+
75+
最后将栈中的元素拼接成字符串返回。
76+
77+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `s` 的长度。
7278

7379
<!-- tabs:start -->
7480

7581
#### Python3
7682

7783
```python
78-
84+
class Solution:
85+
def clearDigits(self, s: str) -> str:
86+
stk = []
87+
for c in s:
88+
if c.isdigit():
89+
stk.pop()
90+
else:
91+
stk.append(c)
92+
return "".join(stk)
7993
```
8094

8195
#### Java
8296

8397
```java
84-
98+
class Solution {
99+
public String clearDigits(String s) {
100+
StringBuilder stk = new StringBuilder();
101+
for (char c : s.toCharArray()) {
102+
if (Character.isDigit(c)) {
103+
stk.deleteCharAt(stk.length() - 1);
104+
} else {
105+
stk.append(c);
106+
}
107+
}
108+
return stk.toString();
109+
}
110+
}
85111
```
86112

87113
#### C++
88114

89115
```cpp
90-
116+
class Solution {
117+
public:
118+
string clearDigits(string s) {
119+
string stk;
120+
for (char c : s) {
121+
if (isdigit(c)) {
122+
stk.pop_back();
123+
} else {
124+
stk.push_back(c);
125+
}
126+
}
127+
return stk;
128+
}
129+
};
91130
```
92131
93132
#### Go
94133
95134
```go
135+
func clearDigits(s string) string {
136+
stk := []byte{}
137+
for i := range s {
138+
if s[i] >= '0' && s[i] <= '9' {
139+
stk = stk[:len(stk)-1]
140+
} else {
141+
stk = append(stk, s[i])
142+
}
143+
}
144+
return string(stk)
145+
}
146+
```
96147

148+
#### TypeScript
149+
150+
```ts
151+
function clearDigits(s: string): string {
152+
const stk: string[] = [];
153+
for (const c of s) {
154+
if (!isNaN(parseInt(c))) {
155+
stk.pop();
156+
} else {
157+
stk.push(c);
158+
}
159+
}
160+
return stk.join('');
161+
}
97162
```
98163

99164
<!-- tabs:end -->

solution/3100-3199/3174.Clear Digits/README_EN.md

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,97 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Cl
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Stack + Simulation
70+
71+
We use a stack `stk` to simulate this process. We traverse the string `s`. If the current character is a digit, we pop the top element from the stack. Otherwise, we push the current character into the stack.
72+
73+
Finally, we concatenate the elements in the stack into a string and return it.
74+
75+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string `s`.
7076

7177
<!-- tabs:start -->
7278

7379
#### Python3
7480

7581
```python
76-
82+
class Solution:
83+
def clearDigits(self, s: str) -> str:
84+
stk = []
85+
for c in s:
86+
if c.isdigit():
87+
stk.pop()
88+
else:
89+
stk.append(c)
90+
return "".join(stk)
7791
```
7892

7993
#### Java
8094

8195
```java
82-
96+
class Solution {
97+
public String clearDigits(String s) {
98+
StringBuilder stk = new StringBuilder();
99+
for (char c : s.toCharArray()) {
100+
if (Character.isDigit(c)) {
101+
stk.deleteCharAt(stk.length() - 1);
102+
} else {
103+
stk.append(c);
104+
}
105+
}
106+
return stk.toString();
107+
}
108+
}
83109
```
84110

85111
#### C++
86112

87113
```cpp
88-
114+
class Solution {
115+
public:
116+
string clearDigits(string s) {
117+
string stk;
118+
for (char c : s) {
119+
if (isdigit(c)) {
120+
stk.pop_back();
121+
} else {
122+
stk.push_back(c);
123+
}
124+
}
125+
return stk;
126+
}
127+
};
89128
```
90129
91130
#### Go
92131
93132
```go
133+
func clearDigits(s string) string {
134+
stk := []byte{}
135+
for i := range s {
136+
if s[i] >= '0' && s[i] <= '9' {
137+
stk = stk[:len(stk)-1]
138+
} else {
139+
stk = append(stk, s[i])
140+
}
141+
}
142+
return string(stk)
143+
}
144+
```
94145

146+
#### TypeScript
147+
148+
```ts
149+
function clearDigits(s: string): string {
150+
const stk: string[] = [];
151+
for (const c of s) {
152+
if (!isNaN(parseInt(c))) {
153+
stk.pop();
154+
} else {
155+
stk.push(c);
156+
}
157+
}
158+
return stk.join('');
159+
}
95160
```
96161

97162
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string clearDigits(string s) {
4+
string stk;
5+
for (char c : s) {
6+
if (isdigit(c)) {
7+
stk.pop_back();
8+
} else {
9+
stk.push_back(c);
10+
}
11+
}
12+
return stk;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func clearDigits(s string) string {
2+
stk := []byte{}
3+
for i := range s {
4+
if s[i] >= '0' && s[i] <= '9' {
5+
stk = stk[:len(stk)-1]
6+
} else {
7+
stk = append(stk, s[i])
8+
}
9+
}
10+
return string(stk)
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String clearDigits(String s) {
3+
StringBuilder stk = new StringBuilder();
4+
for (char c : s.toCharArray()) {
5+
if (Character.isDigit(c)) {
6+
stk.deleteCharAt(stk.length() - 1);
7+
} else {
8+
stk.append(c);
9+
}
10+
}
11+
return stk.toString();
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def clearDigits(self, s: str) -> str:
3+
stk = []
4+
for c in s:
5+
if c.isdigit():
6+
stk.pop()
7+
else:
8+
stk.append(c)
9+
return "".join(stk)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function clearDigits(s: string): string {
2+
const stk: string[] = [];
3+
for (const c of s) {
4+
if (!isNaN(parseInt(c))) {
5+
stk.pop();
6+
} else {
7+
stk.push(c);
8+
}
9+
}
10+
return stk.join('');
11+
}

0 commit comments

Comments
 (0)