Plus One leetcode

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.

 

Subscribe to see which companies asked this question

vector<int> plusOne(vector<int>& digits) {
    int add = 1;
    int size = digits.size();
    while (size-- && add > 0)
    {
        int num = digits[size] + add;
        add = num / 10;
        digits[size] = num % 10;
    }
    if (add > 0)
        digits.insert(digits.begin(), add);
    return digits;
}

 

posted @ 2016-01-07 20:19  sdlwlxf  阅读(122)  评论(0编辑  收藏  举报