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.
这个就是看第一位会不会被进位,如果被进位了,数组得重新生成。
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
bool carry = true;
int length = digits.size();
for (int i = length - 1; i >= 0; i--){
if (carry){
digits[i]++;
if (digits[i] == 10){
digits[i] = 0;
carry = true;
}
else{
return digits;
}
}
}
if (carry){
vector<int> result;
result.push_back(1);
for (size_t i = 0; i < length; i++){
result.push_back(0);
}
return result;
}
}
};