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

LeetCode Plus One

时间:2015-03-30 16:33:20      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:

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> ans(digits);
        reverse(ans.begin(),ans.end());

        int one = 1;
        for (int i = 0; i < ans.size(); i++) {
            ans[i] += one;
            one = ans[i] / 10;
            ans[i] %= 10;
        }

        if (one != 0) 
            ans.push_back(one);
        reverse(ans.begin(), ans.end());
        return ans;
    }
};



LeetCode Plus One

标签:

原文地址:http://blog.csdn.net/u011345136/article/details/44751107

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