iq.lab
Python starts when a code cell comes near or you run one
easyLoops and basics target 15 min

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].

Example 1
Inputdigits = [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.

Example 2
Inputdigits = [4, 7, 2]Output[4, 7, 3]

472 + 1 = 473. Only the last digit changes.

Example 3
Inputdigits = [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.

Constraints
  • 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.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.