Can it be better?


  • 0
    A
    vector<int> plusOne(vector<int> &digits) {
        int i = digits.size() - 1;
        while (i > -1 && digits[i] == 9) 
            digits[i--] = 0;
        if (i == -1) {
            digits.push_back(0);
            for (int j = digits.size(); j > 0; --j) 
                digits[j] = digits[j - 1];
            digits[0] = 1;
        } else {
            digits[i] += 1;
        }
        return digits;
    }

Log in to reply
 

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