AC C++ solution, might be simplify somehow...


  • 0
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            for(int i=digits.size()-1; i>=0; i--)
            {
                if(digits[i]==9)
                {
                    if(i!=0)
                        digits[i]=0;
                    else
                    {
                        digits[i]=0;
                        digits.insert(digits.begin(),1);
                        return digits;
                    }
                }
                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.