Leetcode-66 Plus One

Posted 北冥有鱼,南冥有猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode-66 Plus One

leetcode 66. Plus One

[LeetCode] 66. Plus One

[leetcode]66.Plus One

leetcode66 Plus One

[leetcode]66Plus One