[Problem]
Given a number represented as an array of digits, plus one to the number.
[Solution]
说明:版权所有,转载请注明出处。 Coder007的博客
Given a number represented as an array of digits, plus one to the number.
[Solution]
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> res;
int carry = 1;
for(int i = digits.size()-1; i >= 0; --i){
int sum = digits[i] + carry;
carry = sum / 10;
sum = sum % 10;
res.insert(res.begin(), sum);
}
if(carry > 0){
res.insert(res.begin(), carry);
}
return res;
}
};
说明:版权所有,转载请注明出处。 Coder007的博客