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) {
        vector<int>::reverse_iterator r_iter=digits.rbegin();
        while(*r_iter==9){
            *r_iter=0;
            r_iter++;
            if(r_iter==digits.rend()){
                digits.insert(digits.begin(),1);
                return digits;
            }
                
        }
        (*r_iter)++;
        return digits;
    }
};

 

posted @ 2014-11-06 14:44  li303491  阅读(89)  评论(0编辑  收藏  举报