Very Fast C++ Solution, Easy to Understand


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