代码改变世界

leetcode - Plus One

2013-11-02 13:52  张汉生  阅读(157)  评论(0)    收藏  举报

 

 1 class Solution {
 2 public:
 3     // not binary digits !!!!! shit!
 4     vector<int> plusOne(vector<int> &digits) {
 5         // Note: The Solution object is instantiated only once and is reused by each test case.
 6         vector<int> rlt(digits.size()+1,0);
 7         int n = digits.size();
 8         rlt[n]=1;
 9         int i;
10         int carry = 0;
11         for (i=n-1; i>=0; i--){
12             rlt[i+1] += digits[i] + carry;
13             carry = rlt[i+1] /10;
14             rlt[i+1] %= 10;
15         }
16         if (carry>0)
17             rlt[0] = carry;
18         vector<int> realRlt;
19         i = 0;
20         while(rlt[i]<=0)
21             i++;
22         while(i<=n)
23             realRlt.push_back(rlt[i++]);
24         return realRlt;
25     }
26 };