Plus one
You get a whole number (zero or more) as a list digits of its decimal digits, in the order you write them: digits[0] is the leftmost digit, so 472 is [4, 7, 2]. Return the digits of that number plus one, as a new list in the same form. Do not change digits itself.
The list never starts with a 0, except for the number zero, which is [0].
digits = [3, 9, 9]Output[4, 0, 0]399 + 1 = 400. The last 9 plus 1 makes 10, so it becomes 0 and carries 1 to the digit on its left. The middle 9 does the same. The 3 takes the carry and becomes 4.
digits = [4, 7, 2]Output[4, 7, 3]472 + 1 = 473. Only the last digit changes.
digits = [9, 9]Output[1, 0, 0]99 + 1 = 100. Both 9s become 0 and the carry runs off the left end, so the answer has one more digit.
1 ≤ len(digits) ≤ 2 × 105
Every item is a digit from 0 to 9.
No leading 0, except in
[0].
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.