Wrong code but accepted


  • 0
    Q

    I found that my wrong code was accepted.

    class Solution {
    public:
      string removeKdigits(string num, int k) {
        if (k == 0)
          return num;
        int len = num.size();
        string ret;
        ret.push_back(num[0]);
        for (int i = 1; i < len; i++) {
          while (ret.size() != 0 && ret.back() > num[i] && k-- > 0) {
            ret.pop_back();
          }
          if (ret.size() != 0 || num[i] != '0') {
            ret.push_back(num[i]);
          }
        }
        ret.resize(len - k);//there shoud  minus the origin k
        if (ret.size() == 0) {
          return "0";
        }
        return ret;
      }
    };
    

Log in to reply
 

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