class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num string字符串
* @param k int整型
* @return string字符串
*/
string removeKnums(string num, int k) {
// write code here
stack<int> stk;
int cur = 0;
int cur_k = 0;
while(cur < num.length()){
while(!stk.empty() && (num[cur]-'0') < stk.top() && cur_k < k){
stk.pop();
cur_k++;
}
stk.push(num[cur] - '0');
cur++;
}
while(cur_k < k){
stk.pop();
cur_k++;
}
string res = "";
while(!stk.empty()){
res.push_back(stk.top()+'0');
stk.pop();
}
// 去除前导0
cur = res.length()-1;
while(res[cur] == '0'){
res.pop_back();
cur--;
}
reverse(res.begin(), res.end());
return res == ""? "0":res;
}
};