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

LeetCode "Plus One"

时间:2014-07-22 00:33:36      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   re   c   

The trick is, we can work on a reversed vector - learnt from EPI.

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        std::reverse(digits.begin(), digits.end());
        int i = 0;
        digits[i] += 1;
        while (digits[i] >= 10)
        {
            digits[i++] = 0;
            if (i == digits.size())
            {
                digits.push_back(0);
            }
            digits[i] += 1;
        }
        std::reverse(digits.begin(), digits.end());
        return digits;
    }
};

LeetCode "Plus One",布布扣,bubuko.com

LeetCode "Plus One"

标签:style   blog   color   io   re   c   

原文地址:http://www.cnblogs.com/tonix/p/3857937.html

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