leetcode-66-plus one

Problem Description:

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.

题目大意:

给一个数组表示一个数字,返回它加一的结果。

Solutions:

很简单的一道题,注意进位的处理,以及全9的情况即可。

Code in C++:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int i = digits.size()-1;
        while(i>=0)
        {
            if(digits[i]<9){
                digits[i]++;
                return digits;
            }
            digits[i--]=0;

        }
        digits.insert(digits.begin(),1);
        return digits;

    }
};