[leetcode] 66. Plus One
题目链接:https://leetcode.com/problems/plus-one/
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) {
int flag = 1;
for(auto it = digits.rbegin(); it != digits.rend();it++){
int sum = *it + flag;
flag = sum / 10;
*it = sum % 10;
}
if(flag)
digits.insert(digits.begin(),flag);
return digits;
}
};