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

[LeetCode] Plus One

时间:2014-07-03 11:53:07      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   for   io   

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

标签:style   blog   color   os   for   io   

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

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