Skip to content

Commit e0772cc

Browse files
authored
Merge pull request #841 from 0xff-dev/402
Add solution and test-cases for problem 402
2 parents 7eccf9f + 45022a9 commit e0772cc

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

leetcode/401-500/0402.Remove-K-Digits/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [402.Remove K 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+
Given string num representing a non-negative integer `num`, and an integer `k`, return the smallest possible integer after removing `k` digits from `num`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: num = "1432219", k = 3
10+
Output: "1219"
11+
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
1312
```
1413

15-
## 题意
16-
> ...
17-
18-
## 题解
14+
**Example 2:**
1915

20-
### 思路1
21-
> ...
22-
Remove K Digits
23-
```go
16+
```
17+
Input: num = "10200", k = 1
18+
Output: "200"
19+
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
2420
```
2521

22+
**Example 3:**
23+
24+
```
25+
Input: num = "10", k = 2
26+
Output: "0"
27+
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
28+
```
2629

2730
## 结语
2831

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(num string, k int) string {
4+
n := len(num)
5+
if n == k {
6+
return "0"
7+
}
8+
stack := make([]byte, 0)
9+
for idx := 0; idx < n; idx++ {
10+
for k > 0 && len(stack) > 0 && stack[len(stack)-1] > num[idx] {
11+
stack = stack[:len(stack)-1]
12+
k--
13+
}
14+
stack = append(stack, num[idx])
15+
}
16+
stack = stack[:len(stack)-k]
17+
i := 0
18+
for ; i < len(stack) && stack[i] == '0'; i++ {
19+
}
20+
if i == len(stack) {
21+
return "0"
22+
}
23+
return string(stack[i:])
524
}

leetcode/401-500/0402.Remove-K-Digits/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
k int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "1432219", 3, "1219"},
18+
{"TestCase2", "10200", 1, "200"},
19+
{"TestCase3", "101533485478947384734728478473847", 12, "13334734728478473847"},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)