We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 85b5096 commit f5221fbCopy full SHA for f5221fb
solution/0402.Remove K Digits/Solution.java
@@ -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