Leetcode: Plus One

Question:

Given a number represented as an array of digits, plus one to the number.


My Original solver:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (digits.size() == 0 )
 7             return digits;
 8         int up = 0;
 9         for (vector<int>::iterator it = --digits.end() ;it >= digits.begin(); it-- )
10         {
11             if (it == digits.end() - 1)
12             {
13                 *it = *it + 1;
14                 up = (*it) / 10;
15                 *it = (*it) % 10;
16             }
17             else
18             {
19                 *it = *it + up;
20                 up = (*it) / 10;
21                 *it = (*it) % 10;
22             }
23             
24         }
25         if (up > 0)
26         {
27             digits.insert(digits.begin(), up);
28                 
29         }
30         return digits;
31     }
32 };

改进后的算法:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (digits.size() == 0 )
 7             return digits;
 8         int up = 1;
 9         for (vector<int>::iterator it = --digits.end() ;it >= digits.begin(); it-- )
10         {
11                 *it = *it + up;
12                 up = (*it) / 10;
13                 *it = (*it) % 10;
14             
15         }
16         if (up > 0)
17         {
18             digits.insert(digits.begin(), up);
19                 
20         }
21         return digits;
22     }
23 };

不用迭代器使用数组下标写法:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         vector<int> result(digits);
 7         int size = result.size();
 8         int carry = 1;
 9         for (int i = size - 1; i >= 0; i--)
10         {
11             result[i] = digits[i] + carry;
12             carry = result[i] / 10;
13             result[i] = result[i] % 10;
14         }
15         if (carry > 0)
16             result.insert(result.begin(), carry);
17         return result;
18     }
19 };

解法三:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         vector<int> result(digits);
 7         reverse(result.begin(), result.end());
 8         int size = result.size();
 9         int carry = 1;
10         for (int i = 0; i < result.size(); i++)
11         {
12             result[i] = result[i] + carry;
13             carry = result[i] / 10;
14             result[i] = result[i] % 10;
15         }
16         if (carry > 0)
17             result.push_back(carry);
18         
19         reverse(result.begin(), result.end());
20         return result;
21     }
22 };

 

posted @ 2013-05-02 13:26  caijinlong  阅读(134)  评论(0编辑  收藏  举报