题目描述
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
解题思路
大数加法。 由于只有9 + 1才会发生进位,所以特殊处理这个就好了。
AC代码
class Solution {
public:
vector<int> plusOne(
vector<int>& digits) {
vector<int> ans = digits;
int i = ans.size() -
1;
for (; i >=
0; --i) {
if (ans[i] ==
9) {
ans[i] =
0;
}
else {
ans[i] +=
1;
break;
}
}
if (i <
0)
ans.insert(ans.begin(),
1);
return ans;
}
};
转载请注明原文地址: https://ju.6miu.com/read-17028.html