Skip to content
CodeShare
All problems

Plus One

Add one to a number stored as an array of digits.

EasyArraysMath

A large integer is stored as an array digits, most significant digit first, with no leading zeros (except for the number 0 itself).

Add one to the integer and return the resulting array of digits.

Examples

Example 1
Input
digits = [1, 2, 3]
Output
[1, 2, 4]
Why
123 + 1 = 124.
Example 2
Input
digits = [4, 3, 2, 1]
Output
[4, 3, 2, 2]
Example 3
Input
digits = [9]
Output
[1, 0]
Why
9 + 1 = 10.

Constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9