首页 > 其他 > 详细

LeetCode:Plus One

时间:2014-07-21 09:13:57      阅读:154      评论: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 public:
 3     vector<int> plusOne(vector<int> &digits){
 4         int n=digits.size(),i;
 5         for(i=n-1;i>=0;i--)
 6         {
 7             if(digits[i]==9)
 8             {
 9                 digits[i]=0;
10                 if(i==0)
11                 {
12                     vector<int> temp(digits);
13                     digits.clear();
14                     digits.push_back(1);
15                     for(int j=0;j<n;j++)
16                     {
17                         digits.push_back(temp[j]);
18                     }
19                     break;
20                 }
21             }
22             else
23             {
24                 digits[i]++;
25                 break;
26             }
27         }
28         return digits;
29     }
30 };

LeetCode:Plus One,布布扣,bubuko.com

LeetCode:Plus One

原文:http://www.cnblogs.com/levicode/p/3856322.html

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