Math Number tricks
88. Plus One
You are given a non-negative integer written as an array digits. Each element is one decimal digit, and
the most significant digit comes first, so [4, 0, 7] is the number 407.
Add one to the number and return the result as an array in the same form: one digit per element, most significant digit first, with no leading zeros.
Example 1
Input: digits = [6, 0, 9, 9]
[6,1,0,0]Explanation: The number is 6,099, and 6,099 + 1 = 6,100. The two 9s at the end become 0s, the 0 before them becomes a 1, and the 6 stays as it is.
Example 2
Input: digits = [9, 9, 9]
[1,0,0,0]Explanation: 999 + 1 = 1,000. The answer has one more digit than the input.
Example 3
Input: digits = [0]
[1]Explanation: The number 0 is written as [0], the only input that may start with a 0. Adding one gives 1.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ digits.length ≤ 1050 ≤ digits[i] ≤ 9digitshas no leading zeros, except for the number 0 itself, which is[0].
Clarifying questions
What you would ask an interviewer before writing any code.