-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveKDigitsSmaller.cpp
More file actions
39 lines (33 loc) · 965 Bytes
/
RemoveKDigitsSmaller.cpp
File metadata and controls
39 lines (33 loc) · 965 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
#include<import.h>
class Solution {
public:
string removeKdigits(string num, int k) {
int drop = k;
int len = num.length();
cout << len <<endl;
if(k >= len) return "0";
vector<int> res;
for(int i =0; i< len; i++){
int numb = num[i] - '0';
while(drop && !res.empty() && res.back() > numb){
res.pop_back();
drop--;
}
res.push_back(numb);
}
res.resize(len-k);
// Output the Value in form of string.
string output="";
bool leadZero= true;
for(int num:res){
if(leadZero && num == 0){
continue;
}
else{
leadZero = false;
output += to_string(num);
}
}
return output.empty()?"0":output;
}
};