LeetCode 第 66 题 (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.
这道题属于比较简单的那种。只要模拟手算加法的过程就行了。下面是代码。
vector<int> plusOne(vector<int>& digits)
{
int carry = 1;
for(int i = digits.size() - 1; i > -1 ; i--)
{
int val = carry + digits[i];
carry = (val) / 10;
digits[i] = (val) % 10;
}
if(carry)
{
digits.insert (digits.begin(), carry);
}
return digits;
}