Problem · Array

Add One to a Number Represented as Digits

Learn this problem
EasyOracle logoOracleFULLTIMEONSITE INTERVIEW

Problem statement

You are given a non-empty integer array digits representing a non-negative decimal integer. The most significant digit appears first, and each array element is one digit.

Add one to the represented integer and return the resulting digit array. Do not convert the entire input to a numeric type or use a dynamic list.

The input has no leading zero unless it represents zero itself.

Function

plusOne(digits: int[]) → int[]

Examples

Example 1

digits = [1,2,9]return = [1,3,0]

Example 2

digits = [9,9,9]return = [1,0,0,0]

The carry creates a new most-significant digit.

Constraints

  • 1 <= digits.length <= 200000
  • 0 <= digits[i] <= 9
  • digits[0] != 0 unless digits.length = 1

More Oracle problems

drafts saved locally
public int[] plusOne(int[] digits) {
  // Write your code here.
}
digits[1,2,9]
expected[1,3,0]
checking account