首页 > 其他 > 详细

【leetcode】plus One

时间:2014-04-22 12:41:41      阅读:433      评论: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.

很简单就是像写大数加法那样,代码如下

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         int l=digits.size();
 5         if(l==0){
 6             return digits;
 7         }
 8         if(l==1){
 9             if(digits[0]!=9){
10                 digits[0]++;
11                 return digits;
12             }
13             else{
14                 vector<int> v;
15                 v.push_back(1);
16                 v.push_back(0);
17                 return v;
18             }
19         }
20         int flg=0;
21         int temp=digits[l-1]+1;
22         if(temp>9){
23             digits[l-1]=0;
24             flg=1;
25         }
26         else{
27             digits[l-1]=temp;
28             return digits;
29         }
30         for(int i=l-2;i>=0;i--){
31             int temp=digits[i]+1;
32             if(temp>9){
33                 digits[i]=0;
34                 flg=1;
35             }
36             else{
37                 digits[i]=temp;
38                 return digits;
39             }
40         }
41         if(flg){
42             //vector<int>::iterator it=;
43             digits.insert(digits.begin(),1);
44         }
45         return digits;
46     }
47 };
bubuko.com,布布扣

 

【leetcode】plus One,布布扣,bubuko.com

【leetcode】plus One

原文:http://www.cnblogs.com/MrLJC/p/3679533.html

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