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;
    }
};

 

posted @ 2016-11-21 16:59  冯兴伟  阅读(169)  评论(0编辑  收藏  举报