首页 > 代码库 > Leetcode-66 Plus One

Leetcode-66 Plus One

#66.   Plus One          

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

题解:模拟加法运算就好了。

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int c=1;
        for(int i=digits.size()-1;i>=0;i--)
        {
            int sum=digits[i]+c;
            c=sum/10;
            digits[i]=sum%10;
        }
        if(c==1)
        {
            digits.insert(digits.begin(),1);
        }
        return digits;
    }
};

 

Leetcode-66 Plus One