Skip to content

Commit 0d9a26c

Browse files
committed
Add solution and test-cases for problem 3174
1 parent d569e0c commit 0d9a26c

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

leetcode/3101-3200/3174.Clear-Digits/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [3174.Clear Digits][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a string `s`.
5+
6+
Your task is to remove **all** digits by doing this operation repeatedly:
7+
8+
- Delete the first digit and the **closest non-digit** character to its left.
9+
10+
Return the resulting string after removing all digits.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
15+
Input: s = "abc"
16+
17+
Output: "abc"
1418
15-
## 题意
16-
> ...
19+
Explanation:
1720
18-
## 题解
21+
There is no digit in the string.
22+
```
23+
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Clear Digits
23-
```go
2426
```
27+
Input: s = "cb34"
28+
29+
Output: ""
2530
31+
Explanation:
32+
33+
First, we apply the operation on s[2], and s becomes "c4".
34+
35+
Then we apply the operation on s[1], and s becomes "".
36+
```
2637

2738
## 结语
2839

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) string {
4+
bs := []byte(s)
5+
index := -1
6+
for i := range len(bs) {
7+
if !(bs[i] >= '0' && bs[i] <= '9') {
8+
index++
9+
bs[index] = bs[i]
10+
continue
11+
}
12+
index--
13+
}
14+
return string(bs[:index+1])
515
}

leetcode/3101-3200/3174.Clear-Digits/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abc", "abc"},
17+
{"TestCase2", "cb34", ""},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)