首页 > 代码库 > LeetCode --- Plus One

LeetCode --- Plus One

题目链接

题意:给出一个以数组形式表示的数, 求该数加1后的结果,同样以数组形式返回。

附上代码:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         unsigned int len = digits.size();
 5         for (unsigned int i = 0; i < len/2; i++) {
 6             swap(digits[i], digits[len-1-i]);
 7         }
 8         int c = 1;
 9         for (int i = 0; i < len and c; i++) {
10             int a = digits[i] + c;
11             c = a / 10;
12             digits[i] = a % 10;
13         }
14         if (c) {
15             digits.push_back(1);
16             len++;
17         }
18         for (unsigned int i = 0; i < len/2; i++) {
19             swap(digits[i], digits[len-1-i]);
20         }
21         
22         return digits;
23     }
24 };