本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

LeetCode:Plus One

题目链接

Given a number represented as an array of digits, plus one to the number

分析:too simple                                               本文地址

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int len = digits.size();
        vector<int> res(digits);
        int carrybit = 1, i;
        for(i = len-1; i >= 0; i--)
        {
            if(carrybit == 0)break;
            else 
            {
                res[i] += carrybit;
                if(res[i] >= 10)
                {
                    carrybit = 1;
                    res[i] -= 10;
                }
                else carrybit = 0;
            }
        }
        if(i < 0 && carrybit == 1)
            res.insert(res.begin(), carrybit);
        return res;
    }
};

 

【版权声明】转载请注明出处http://www.cnblogs.com/TenosDoIt/p/3475284.html

posted @ 2013-12-15 14:58  tenos  阅读(622)  评论(0编辑  收藏  举报

本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

公益页面-寻找遗失儿童