Menu
CoddyTech

Plus One

A non-negative whole number is stored as an array of its decimal digits, digits, most significant digit first: 472 is [4, 7, 2]. Add one to the number and return the digits of the result in the same form. The number can have up to 100 digits, far more than a 64-bit integer holds.

Function

plusOne(digits: integer-array) → integer-array
digitsinteger-array
the digits of the number, most significant first
Returnsinteger-array
the digits of the number plus one, most significant first

Constraints

  • 1 ≤ digits.length ≤ 100
  • 0 ≤ digits[i] ≤ 9
  • digits has no leading zero, except for the number 0 itself, which is [0].

Examples

Input
digits = [4, 3, 9]
Output
[4, 4, 0]
Explanation
The number is 439, and 439 + 1 = 440. The last digit 9 turns into 0 and passes a carry to the 3, which becomes 4.
challenge icon

Follow-up

How would you subtract one instead, for a number of at least 1? When does the result lose its leading digit, as in [1, 0, 0]?

Reset code
def plusOne(digits):
    # Write code here

Test #1

Test #2

Test #3

Argument

digits = [4, 3, 9]

Expected Output

[4, 4, 0]