Skip to content
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
37 changes: 24 additions & 13 deletions leetcode/3101-3200/3174.Clear-Digits/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
# [3174.Clear Digits][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a string `s`.

Your task is to remove **all** digits by doing this operation repeatedly:

- Delete the first digit and the **closest non-digit** character to its left.

Return the resulting string after removing all digits.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: s = "abc"

Output: "abc"

## 题意
> ...
Explanation:

## 题解
There is no digit in the string.
```

**Example 2:**

### 思路1
> ...
Clear Digits
```go
```
Input: s = "cb34"

Output: ""

Explanation:

First, we apply the operation on s[2], and s becomes "c4".

Then we apply the operation on s[1], and s becomes "".
```

## 结语

Expand Down
14 changes: 12 additions & 2 deletions leetcode/3101-3200/3174.Clear-Digits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) string {
bs := []byte(s)
index := -1
for i := range len(bs) {
if !(bs[i] >= '0' && bs[i] <= '9') {
index++
bs[index] = bs[i]
continue
}
index--
}
return string(bs[:index+1])
}
13 changes: 6 additions & 7 deletions leetcode/3101-3200/3174.Clear-Digits/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "abc", "abc"},
{"TestCase2", "cb34", ""},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading