LeetCode #402

Remove K Digits

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    string removeKdigits(string num, int k) {
        stack<char> st;
        for(char ch: num) {
            while(k > 0 && !st.empty() && st.top() > ch) st.pop(), --k;
            st.push(ch);
        }
        string ret = "";
        while(!st.empty()) ret += st.top(), st.pop();
        while(!ret.empty() && ret.back() == '0') ret.pop_back();
        reverse(ret.begin(), ret.end());
        while(!ret.empty() && k > 0) ret.pop_back(), --k;
        if(ret.empty()) ret = "0";
        return ret;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.