diff --git a/leetcode/3101-3200/3174.Clear-Digits/README.md b/leetcode/3101-3200/3174.Clear-Digits/README.md index 814084c11..43536a572 100755 --- a/leetcode/3101-3200/3174.Clear-Digits/README.md +++ b/leetcode/3101-3200/3174.Clear-Digits/README.md @@ -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 "". +``` ## 结语 diff --git a/leetcode/3101-3200/3174.Clear-Digits/Solution.go b/leetcode/3101-3200/3174.Clear-Digits/Solution.go index d115ccf5e..c902bcf30 100644 --- a/leetcode/3101-3200/3174.Clear-Digits/Solution.go +++ b/leetcode/3101-3200/3174.Clear-Digits/Solution.go @@ -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]) } diff --git a/leetcode/3101-3200/3174.Clear-Digits/Solution_test.go b/leetcode/3101-3200/3174.Clear-Digits/Solution_test.go index 14ff50eb4..2396b1b49 100644 --- a/leetcode/3101-3200/3174.Clear-Digits/Solution_test.go +++ b/leetcode/3101-3200/3174.Clear-Digits/Solution_test.go @@ -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", ""}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }