-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveKthElement.java
39 lines (30 loc) · 977 Bytes
/
RemoveKthElement.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
public class Solution {
public String removeKdigits(String num, int k) {
if(k==0)
return num;
if(k==num.length())
return 0+"";
Deque<Character> deque = new LinkedList<Character>();
char[] c= num.toCharArray();
for(int i = 0;i<c.length;i++){
while(deque.size()!=0 && c[i]<deque.peekLast() && k!=0)
{
k--;
deque.removeLast();
}
deque.add(c[i]);
}
for(int i = 0;i<k;i++)
if(deque.size()>0)
deque.removeLast();
StringBuilder output= new StringBuilder();
for(Character ch: deque){
output.append(ch);
}
while(output.length() > 1 && output.charAt(0) == '0'){
output.deleteCharAt(0);
}
return output.toString();
}
}class RemoveKthElement {
}