Leetcode 66. Plus One (Easy) (cpp)
Tag: Array, Math
Difficulty: Easy
/*
66. Plus One (Easy)
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) {
reverse(digits.begin(), digits.end());
int flag = 1;
for (int i = 0; i < digits.size(); i++) {
digits[i] += flag;
flag = digits[i] / 10;
digits[i] %= 10;
}
if (flag) {
digits.push_back(1);
}
reverse(digits.begin(), digits.end());
return digits;
}
};