Skip to content

feat: add solutions to lc problems: No.3174,3175,3182 #3086

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 10, 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 @@ -80,6 +80,10 @@ tags:

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

相似题目:

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

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ So we can see that 4 rounds will be played and 5 is the winner because it wins 2

<!-- solution:start -->

### Solution 1
### Solution 1: Quick Thinking

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.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

Similar problems:

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

<!-- tabs:start -->

Expand Down
73 changes: 69 additions & 4 deletions solution/3100-3199/3174.Clear Digits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,97 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Cl

<!-- solution:start -->

### 方法一
### 方法一:栈 + 模拟

我们用一个栈 `stk` 来模拟这个过程,遍历字符串 `s`,如果当前字符是数字,就弹出栈顶元素,否则将当前字符入栈。

最后将栈中的元素拼接成字符串返回。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `s` 的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def clearDigits(self, s: str) -> str:
stk = []
for c in s:
if c.isdigit():
stk.pop()
else:
stk.append(c)
return "".join(stk)
```

#### Java

```java

class Solution {
public String clearDigits(String s) {
StringBuilder stk = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
stk.deleteCharAt(stk.length() - 1);
} else {
stk.append(c);
}
}
return stk.toString();
}
}
```

#### C++

```cpp

class Solution {
public:
string clearDigits(string s) {
string stk;
for (char c : s) {
if (isdigit(c)) {
stk.pop_back();
} else {
stk.push_back(c);
}
}
return stk;
}
};
```

#### Go

```go
func clearDigits(s string) string {
stk := []byte{}
for i := range s {
if s[i] >= '0' && s[i] <= '9' {
stk = stk[:len(stk)-1]
} else {
stk = append(stk, s[i])
}
}
return string(stk)
}
```

#### TypeScript

```ts
function clearDigits(s: string): string {
const stk: string[] = [];
for (const c of s) {
if (!isNaN(parseInt(c))) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.join('');
}
```

<!-- tabs:end -->
Expand Down
73 changes: 69 additions & 4 deletions solution/3100-3199/3174.Clear Digits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,97 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3174.Cl

<!-- solution:start -->

### Solution 1
### Solution 1: Stack + Simulation

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.

Finally, we concatenate the elements in the stack into a string and return it.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string `s`.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def clearDigits(self, s: str) -> str:
stk = []
for c in s:
if c.isdigit():
stk.pop()
else:
stk.append(c)
return "".join(stk)
```

#### Java

```java

class Solution {
public String clearDigits(String s) {
StringBuilder stk = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
stk.deleteCharAt(stk.length() - 1);
} else {
stk.append(c);
}
}
return stk.toString();
}
}
```

#### C++

```cpp

class Solution {
public:
string clearDigits(string s) {
string stk;
for (char c : s) {
if (isdigit(c)) {
stk.pop_back();
} else {
stk.push_back(c);
}
}
return stk;
}
};
```

#### Go

```go
func clearDigits(s string) string {
stk := []byte{}
for i := range s {
if s[i] >= '0' && s[i] <= '9' {
stk = stk[:len(stk)-1]
} else {
stk = append(stk, s[i])
}
}
return string(stk)
}
```

#### TypeScript

```ts
function clearDigits(s: string): string {
const stk: string[] = [];
for (const c of s) {
if (!isNaN(parseInt(c))) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.join('');
}
```

<!-- tabs:end -->
Expand Down
14 changes: 14 additions & 0 deletions solution/3100-3199/3174.Clear Digits/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
string clearDigits(string s) {
string stk;
for (char c : s) {
if (isdigit(c)) {
stk.pop_back();
} else {
stk.push_back(c);
}
}
return stk;
}
};
11 changes: 11 additions & 0 deletions solution/3100-3199/3174.Clear Digits/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func clearDigits(s string) string {
stk := []byte{}
for i := range s {
if s[i] >= '0' && s[i] <= '9' {
stk = stk[:len(stk)-1]
} else {
stk = append(stk, s[i])
}
}
return string(stk)
}
13 changes: 13 additions & 0 deletions solution/3100-3199/3174.Clear Digits/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public String clearDigits(String s) {
StringBuilder stk = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
stk.deleteCharAt(stk.length() - 1);
} else {
stk.append(c);
}
}
return stk.toString();
}
}
9 changes: 9 additions & 0 deletions solution/3100-3199/3174.Clear Digits/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def clearDigits(self, s: str) -> str:
stk = []
for c in s:
if c.isdigit():
stk.pop()
else:
stk.append(c)
return "".join(stk)
11 changes: 11 additions & 0 deletions solution/3100-3199/3174.Clear Digits/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function clearDigits(s: string): string {
const stk: string[] = [];
for (const c of s) {
if (!isNaN(parseInt(c))) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.join('');
}
Loading