[Problem]
Given a number represented as an array of digits, plus one to the number.
[Solution]
Given a number represented as an array of digits, plus one to the number.
[Solution]
class Solution {说明:版权所有,转载请注明出处。 Coder007的博客
public:
vector<int> plusOne(vector<int> &digits) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<int> res;
int carry = 1;
// add from the end to the front
for(int i = digits.size()-1; i >= 0; --i){
int sum = digits[i] + carry;
carry = sum / 10;
sum %= 10;
res.insert(res.begin(), sum);
}
// add the last carry
if(carry > 0){
res.insert(res.begin(), carry);
}
return res;
}
};