1. 题目来源
链接:66. 加一
2. 题目解析
前导题:
就是高精度加法。模板题。
- 时间复杂度: O ( n ) O(n) O(n)。
- 空间复杂度: O ( 1 ) O(1) O(1)
代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
reverse(digits.begin(), digits.end());
int t = 1;
for (int i = 0; i < digits.size(); i ++ ) {
t += digits[i];
digits[i] = t % 10;
t /= 10;
}
if (t) digits.push_back(t);
reverse(digits.begin(), digits.end());
return digits;
}
};