-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRemove K Digits.cpp
More file actions
42 lines (37 loc) · 1003 Bytes
/
Remove K Digits.cpp
File metadata and controls
42 lines (37 loc) · 1003 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
class Solution {
public:
string removeKdigits(string num, int k) {
int len = num.length();
if(k==len){
return "0";
}
string small = "",pick;
int no_times = len-k;
for(int i=0;i<no_times;i++){
pick = num.substr(0,k+1);
int minn = 0;
for(int j=1;j<pick.length();j++){
if(pick[j]<pick[minn]){
minn = j;
}
}
small += pick[minn];
if(minn+1<num.length()){
num = num.substr(minn+1);
}
k=k-minn;
}
int last = -1;
for(int i=0;i<small.length();i++){
if(small[i]=='0' && last == i-1){
last = i;
}else{
break;
}
}
if(last==small.length()-1){
return "0";
}
return small.substr(last+1);
}
};