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) {
vector<int> solution;
int addition = 1;
for (int i=digits.size()-1;i>=0;i--){
solution.push_back((digits[i]+addition)%10);
addition = (digits[i]+addition)/10;
}
if (addition>0)
solution.push_back(addition);
reverse(solution.begin(), solution.end());
return solution;
}
};
408

被折叠的 条评论
为什么被折叠?



