码迷,mamicode.com
首页 > 其他好文 > 详细

Plus One

时间:2014-11-06 16:58:34      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   div   on   

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;
    }
};

 

Plus One

标签:style   blog   io   color   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/li303491/p/4078828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!