Plus One — Math & Matrix
Add one to a whole number kept as a digit list, most significant first, and give back the result in identical form.
Math & Matrix · Easy
The problem
A mileage counter keeps its reading as a list of digits, and it is about to tick up by one. A non-negative whole number is stored as a list of its decimal digits, most significant digit first. Add one to the number and return its digits in the same layout.
The story
A car's odometer stores its reading as a list of digits, most significant first, with no leading zero unless the reading is 0. It's about to tick up by one mile. Return the new reading in the same layout; it may come out one digit longer.
What to return
- Return the digits of the number plus one, most significant first.
- The result may be one digit longer than the input.
Constraints
- 1 <= digits.length <= 2500
- 0 <= digits[i] <= 9
- no leading zero, except that the number zero itself is [0]
- Your code may run at most 50,036 counted steps on the largest case.
Example
- Input
digits = [6, 0, 4]- Output
[6, 0, 5]