-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumDeletekMaximum.java
More file actions
44 lines (28 loc) · 912 Bytes
/
NumDeletekMaximum.java
File metadata and controls
44 lines (28 loc) · 912 Bytes
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
package leetcode;
import com.sun.scenario.effect.Offset;
/**
* Created by solie_h on 2018/11/16.
*/
public class NumDeletekMaximum {
public static String removeKDigits(String num, int k) {
int resultLength = num.length() - k;
char[] stack = new char[num.length()];
int top = 0;
for (int i = 0; i < num.length(); i++) {
char c = num.charAt(i);
while (top > 0 && stack[top - 1] > c && k > 0) {
top = top - 1;
k = k - 1;
}
stack[top++] = c;
}
int offset = 0;
while (offset < resultLength && stack[offset] == '0') {
offset++;
}
return offset == resultLength ? "0" : new String(stack, offset, resultLength - offset);
}
public static void main(String[] args) {
System.out.println(removeKDigits("17287", 2));
}
}