Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Input: digits = [9,9,9]
Output: [1,0,0,0]
Explanation: The array represents the integer 999.
vector<int> plusOne(vector<int> &d)
{
int i = d.size() - 1, sum = 0, carry = 0;
sum = d[d.size() - 1] + 1;
carry = sum / 10;
d[d.size() - 1] = sum % 10;
i -= 1;
while (i >= 0 && carry != 0)
{
sum = d[i] + carry;
d[i] = sum % 10;
carry = sum / 10;
i -= 1;
}
vector<int> v;
while (carry != 0)
{
v.push_back(carry % 10);
carry /= 10;
}
for (auto it : d)
v.push_back(it);
return v;
}
Comments
Post a Comment