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

Constraints

Example

Input
digits = [6, 0, 4]
Output
[6, 0, 5]

Solve it

More in Math & Matrix

All algorithm problems