4ms C++ Solution


  • 0
    B
    class Solution {
    

    public:
    vector<int> plusOne(vector<int>& digits)
    {
    int n = digits.size();
    if (n == 0) {return digits;}

        if (digits[n-1] != 9) 
        {digits[n-1] = digits[n-1]+1;}
        else
        {
            for (int i=n-1; i>=0; i--)
            {
                if (digits[i] == 9) 
                {digits[i] = 0;}
                else
                {
                    digits[i] += 1;
                    break;
                }
            }
            if (digits[0] == 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.