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.
Solution:Code:
<span style="font-size:14px;">class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int carry = 1, sum;
const int length = digits.size();
for (int i = length-1; i >= 0; --i) {
sum = carry + digits[i];
carry = sum/10;
digits[i] = sum%10;
}
if (carry) digits.insert(digits.begin(), 1);
return digits;
}
};</span>