Another simple C++ solution


  • 0
    A

    vector<int> plusOne1(vector<int> &digits)
    {

    int i;
        
    for (i = digits.size() - 1; i >= 0 && digits[i] == 9; i--)
    {
        digits[i] = 0;
    }
        
    if (i == -1)
    {
        digits[0] = 1;
        digits.push_back(0);
    }
    else
    {
        digits[i]++;
    }
        
    return digits;
    

    }


Log in to reply
 

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