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> retVtr;
if (digits.size() == 0)
return retVtr;
int len = digits.size();
int carry = 0;
for (int i=len-1; i>=0; i--)
{
int newVal;
if (i == len-1)
newVal = digits[len-1]+1;
else
newVal = digits[i]+carry;
if (newVal >= 10)
{
carry = 1;
newVal = newVal%10;
}
else
{
carry = 0;
}
retVtr.push_back(newVal);
}
if (carry)
{
retVtr.push_back(1);
}
vector<int> result;
for (int i=retVtr.size()-1; i>=0; i--)
result.push_back(retVtr[i]);
return result;
}
};