-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution402.java
42 lines (35 loc) · 1.12 KB
/
Solution402.java
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 移掉K位数字
* @date: 2019/01/20
*/
public class Solution402 {
public static void main(String[] args) {
String num = "1432219";
final int k = 3;
System.out.println(new Solution402().removeKdigits(num, k));
}
public String removeKdigits(String num, int k) {
int digits = num.length() - k;
char[] stack = new char[num.length()];
int top = 0;
// k keeps track of how many characters we can remove
// if the previous character in stk is larger than the current one
// then removing it will get a smaller number
// but we can only do so when k is larger than 0
for (int i = 0; i < num.length(); ++i) {
char c = num.charAt(i);
while (0 < top && 0 < k && c < stack[top - 1]) {
--top;
--k;
}
stack[top++] = c;
}
int idx = 0;
while (idx < digits && '0' == stack[idx]) {
++idx;
}
return idx == digits ? "0" : new String(stack, idx, digits - idx);
}
}