【LeetCode】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 flag = 1;
        int i;
        
        for (i = digits.size() - 1; i >= 0; --i) {
            if (++digits[i] == 10)
            {
                digits[i] = 0;
            }
            else
            {
                return digits;
            }

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

 

posted on 2016-08-23 09:51  医生工程师  阅读(108)  评论(0编辑  收藏  举报

导航