Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1]
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry = 0;
int len = digits.size();
for (int i = len - 1; i >= 0; i--){
if (i == len - 1){
carry = (digits[i] + 1) / 10;
digits[i] = (digits[i] + 1) % 10;
}
else{
digits[i] += carry;
carry = digits[i] / 10;
digits[i] %= 10;
}
}
if (carry == 0){
return digits;
}
else{
vector<int> res;
res.push_back(carry);
for (int i : digits){
res.push_back(i);
}
return res;
}
}
};