Problem · Array
Add One to a Number Represented as Digits
Learn this problemProblem 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 <= 2000000 <= digits[i] <= 9digits[0] != 0unlessdigits.length = 1