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.
//#66 Plus One
//4ms 100%
class Solution {
public:
vector<int> plusOne(vector<int>& digits)
{
if(digits.size() == 0)
{
digits.push_back(1);
return digits;
}
//cout << "ok\n";
int carry(1);
for(vector<int>::reverse_iterator r_it(digits.rbegin()); r_it != digits.rend(); r_it ++)
{
//cout << "ok" << i << endl;
int sum = carry + *r_it;
carry = sum / 10;
*r_it = sum % 10;
}
if(carry == 1)
{
digits.insert(digits.begin(), 1);
}
return digits;
}
};