C++ 4ms simple solution


  • 0
    Z
    class Solution {
    public:
    vector<int> plusOne(vector<int>& digits) {
        return solution1(digits);
    }
    private:
    vector<int> solution1(vector<int> &digits){
        int dig = digits.size() - 1;
        while(dig >= 0){
            if(digits[dig] < 9){
                digits[dig]++; 
                break;
            }
            digits[dig--] = 0;
        }
        if(dig == -1) digits.insert(digits.begin(), 1);
        return digits;
    }
    };

Log in to reply
 

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