注意当输入为空时应该输出1.
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
reverse(digits.begin(), digits.end());
vector<int> ans;
int carry = 0;
for (int i = 0; i < digits.size(); ++i) {
int plus = i == 0 ? 1 : 0;
int now = (plus + digits[i] + carry) % 10;
carry = (plus + digits[i] + carry) / 10;
ans.push_back(now);
}
if (carry) ans.push_back(carry);
reverse(ans.begin(), ans.end());
if (ans.size() == 0) ans.push_back(1);
return ans;
}
};