Skip to content

Commit f5221fb

Browse files
authored
Create Solution.java
1 parent 85b5096 commit f5221fb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public String removeKdigits(String num, int k) {
3+
if (k <= 0) {
4+
return num;
5+
}
6+
if (num.length() <= k) {
7+
return "0";
8+
}
9+
int len = num.length() - k;
10+
char[] cs = new char[num.length()];
11+
int top = -1;
12+
for (char c : num.toCharArray()) {
13+
while (top >= 0 && cs[top] > c && k > 0) {
14+
--top;
15+
--k;
16+
}
17+
cs[++top] = c;
18+
}
19+
int offset = 0;
20+
while (offset <= top && cs[offset] == '0') {
21+
++offset;
22+
}
23+
return offset > top ? "0" : new String(cs, offset, len - offset);
24+
}
25+
}

0 commit comments

Comments
 (0)