-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path402.cpp
37 lines (35 loc) · 831 Bytes
/
402.cpp
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
class Solution {
public:
string removeKdigits(string num, int k) {
int n = num.size();
stack<char> st;
for(auto ch: num){
//cout<<-1<<endl;
while(st.size() && st.top() > ch && k > 0){
k--;
st.pop();
}
if(st.empty() && ch == '0'){
continue;
}
else{
st.push(ch);
}
}
while(!st.empty() && k > 0){
k--;
st.pop();
}
//cout<<st.size()<<endl;
string Ans = "";
while(!st.empty()){
//cout<<-1<<endl;
Ans = st.top() + Ans;
st.pop();
}
if(Ans.size() == 0){
return "0";
}
return Ans;
}
};