Description:
https://leetcode.com/problems/plus-one/#/description
Code:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry = 1;
vector<int>result = digits;
for (int i = digits.size() - 1; i >= 0 ;i--)
{
result[i] = (digits[i] + carry) % 10;
carry = (digits[i] + carry) / 10;
}
if (carry > 0)
{
int end = result[digits.size()-1];
for (int i = 0; i < digits.size(); i++)
{
result[i+1] = result[i];
}
result[0] = 1;
result.push_back(end);
}
return result;
}
};
Timing:
Fastest, 3ms.
O(n)