首页 > 其他 > 详细

[LeetCode] Plus One

时间:2014-07-03 11:52:49      阅读:321      评论: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.

 

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

 

 

 

 

 

[LeetCode] Plus One,布布扣,bubuko.com

[LeetCode] Plus One

原文:http://www.cnblogs.com/diegodu/p/3815561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!