Wednesday, September 24, 2014

plus one

    vector<int> plusOne(vector<int> &digits) {
        int i=digits.size()-1;
        int carry=1;
        while(i>=0&&carry){
            int sum=digits[i]+carry;
            carry=sum/10;
            digits[i]=sum%10;
            i--;
        }
        if(carry)
            digits.insert(digits.begin(),1);
        return digits;   
    }

No comments:

Post a Comment