leetcode 402. Remove K Digits

class Solution {// 考察对数据结构stack 的使用
public:
    string removeKdigits(string num, int k) {
        deque _deque;
        int cnt = 0; 
        for (int i = 0; i < num.size(); ++i){
            while (!_deque.empty() && _deque.back() > num[i] && cnt < k){
                cnt++;
                _deque.pop_back();
            }
            _deque.push_back(num[i]);
        }
        string ret;
        for (int i = 0; i < num.size() - k; ++i){
            if (!ret.empty() || _deque[i] != '0')
                ret += _deque[i];
        }
        return ret.empty() ? "0" : ret;
    }
};

你可能感兴趣的:(stack)