C++ 4ms one path (two when new most significant 1 needs to be added: 99+1=100)


  • 0
    P
    vector<int> plusOne(vector<int>& digits) 
    {
        for(int i = digits.size()-1; i >= 0; --i)
        {
            if(digits[i] < 9)
            {
                digits[i] += 1;
                break;
            }
            else
            {
                digits[i] = 0;
                if(i == 0)
                    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.