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.
题意是数组存储的是一个数的每位数字,然后把这个数字加1,这跟用字符串存储数字是一个道理,按照加法规则从低位开始处理进位,当最高位有进位的时候就插入一位。
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size()-1;
digits[i]++;
while(i > 0){
if(digits[i] >= 10){
digits[i-1]++;
digits[i] -= 10;
}
i--;
}
if(digits[0] >= 10){
digits[0] -= 10;
digits.insert(digits.begin(), 1);
}
return digits;
}
};