Back to ArraysPlus One
Easy

Plus One

EasyOptionalIn-place Manipulation

An array of digits represents a non-negative integer, most significant digit first. Return the array after adding one to the number.

Examples

Example 1

Input:
digits = [1,2,3]
Output:
[1,2,4]

Explanation: 123 + 1 = 124, and only the last digit changes.

Example 2

Input:
digits = [4,3,2,1]
Output:
[4,3,2,2]

Example 3

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

Explanation: 99 + 1 = 100 — the carry propagates through both 9s and the array grows by one digit.

Constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain leading zeros (except the number 0 itself).

Loading editor…

Code execution is coming soon.