C++ 6ms accepted solution


  • 0
    X
        string removeKdigits(string num, int k) {
            if(k <= 0) return num; 
            if(k >= num.size()) return "0";
            while(k-- > 0){
                int i = 0;
                for(; i <num.size() - 1; ++i)
                    if(num[i + 1] < num[i]) break; 
                num.erase(i, 1);
                i = 0;
                for(; i < num.size(); ++i)
                    if(num[i] != '0') break;
                if(i == num.size()) return "0";
                if(i > 0) num.erase(0, i);
            }
            return num; 
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.