LeetCode : Plus One

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

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 tmp = 0;
       for(int i=digits.size()-1;i>=0;--i)
       {
           tmp = digits[i]+flag;
           digits[i] = tmp%10;
           flag=tmp/10;
       }
       if(flag==1)
          digits.insert(digits.begin(),1);
       return digits;
    }
};

posted on 2017-03-28 15:57  gechen  阅读(71)  评论(0编辑  收藏  举报

导航