Thanks a lot, I implement this idea with c++

class Solution { public: string removeKdigits(string num, int k) { int digitsCnt = num.size() - k; vector<char> stack; // push to stack, compare and remove. if (num.empty()) return string("0"); stack.push_back(num[0]); for (unsigned i = 1; i < num.size(); i++) { char c = stack.back(); while (num[i] < c && k > 0) { stack.pop_back(); k--; // if stack is empty, out while loop if(stack.empty()) break; else c = stack.back(); } stack.push_back(num[i]); } // pick first digitsCnt digits from bottom to up of the stack vector<char> digits(stack.begin(), stack.begin() + digitsCnt); unsigned j = 0; while (!digits.empty() && digits[j] == '0') // skip leading zero ++j; // construct result string result(digits.begin() + j, digits.end()); if (result.empty()) return string("0"); return result; } };Remove K Digits