Show problems as
Classic problem #66EasyArrayMath

Plus One

Real-world version: Wheel Counter Tick
Tracked in this browser only
Read Plus One on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A turnstile keeps its running count on a row of mechanical drums, one digit per drum. The array digits reports what the drums currently show, with digits[0] sitting at the far left of the row.

One more person walks through, so the counter ticks up by a single step. A drum showing nine wraps back to zero and nudges the drum to its left; when the leftmost drum wraps, the housing exposes a fresh drum in front of it that now shows one. Give back the drum readings after the tick.

Examples

Example 1

Input
digits = [3, 1, 7]
Output
[3, 1, 8]

The rightmost drum has room to climb, so nothing else on the row moves.

Example 2

Input
digits = [6, 9]
Output
[7, 0]

The rightmost drum wraps to zero and nudges its neighbour from six to seven.

Example 3

Input
digits = [9, 9, 9]
Output
[1, 0, 0, 0]

Every drum wraps, so the housing exposes a new drum in front showing one.

Constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • The reading has no zero drum in front of its first significant drum.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def plus_one(digits: list[int]) -> list[int]:
Java
public int[] plusOne(int[] digits)