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

[LeetCode] Plus One

时间:2015-08-11 11:36:32      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

The idea is just to perform the addition from right to left as usual :-)

Note that the result may be longer than the original digits by 1 number (the carry).

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         int c = 1, n = digits.size();
 5         vector<int> sum(n, 0);
 6         for(int i = n - 1; i >= 0; i--) {
 7             int val = digits[i] + c; 
 8             sum[i] = val % 10;
 9             c = val / 10;
10         }
11         if(c) sum.insert(sum.begin(), c);
12         return sum;
13     }
14 };

 

[LeetCode] Plus One

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4720105.html

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