Leetcode 66 Plus One

2018-01-12 14:59:46 浏览数 (1)

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.

模拟大数加法加一,

注意判断首位是否有进位!

代码语言:javascript复制
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int add=1;
        for(int i=digits.size()-1;i>=0;i--)
        {
             digits[i]=(digits[i] add);
             if(digits[i]!=0) break;
        }
        if(digits[0]==0) digits.insert(digits.begin(),1);
        return digits;
    }
};

0 人点赞