-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
60 lines (48 loc) · 1.49 KB
/
Solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package lc402
import (
"strconv"
"strings"
)
func removeKDigits(num string, k int) string {
if len(num) <= k {
return "0"
}
// Monotonically non-decreasing stack. Represents the final number after removing k digits.
nums := make([]int, 0, len(num))
for _, r := range num {
// Unicode: Characters '0', '1', ..., '9' are encoded consecutively in the Unicode
// table. That is, '0': 48, '1': 49, ..., '9': 57.
// Suppose the rune r represents a digit. Then, taking the unicode difference
// between r and '0' returns the actual integer representation of the digit.
digit := int(r - '0')
// Since the digits are iterated in decreasing significance,
// while there are still digits to be removed (k > 0)
for k > 0 && len(nums) > 0 {
previousDigit := nums[len(nums)-1]
if previousDigit <= digit {
break // simply add digit to the stack
}
// Pop previousDigit and decrement k to maintain monotonicity of stack.
nums = nums[:len(nums)-1]
k--
}
// Numbers do not start with a 0, i.e. discard leading zeros
if len(nums) == 0 && digit == 0 {
continue
}
nums = append(nums, digit)
}
// Case where digits in num are non-decreasing, resulting in no digits removed earlier
if k > 0 {
nums = nums[:len(nums)-min(len(nums), k)] // remove top k digits
}
// No remaining digits that are non-zero
if len(nums) == 0 {
return "0"
}
var result strings.Builder
for _, digit := range nums {
result.WriteString(strconv.Itoa(digit))
}
return result.String()
}