1.问题描述
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer does 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.
2.思考过程
emm...
Talk is cheap, show you my code.
3.代码实现
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int flag = 1;
for(int i=digits.size()-1;i>=0;i--){
if(!flag) break;
int cur = flag + digits[i];
flag = cur / 10;
digits[i] = cur % 10;
}
if(flag){
digits.insert(digits.begin(),flag);
}
return digits;
}
};