Plus One
Easy · math
A non-negative whole number is written as a list of its decimal digits, most significant digit first. For example, 4096 is stored as [4, 0, 9, 6]. The list has no leading zeros, except that the number zero is stored as [0].
Return the digits of that number plus one, in the same format. The number may have hundreds of digits, too many for a built-in integer, so the addition has to be done on the digits themselves.
Examples
Example 1
Input: digits = [1, 2, 3]
Output: [1, 2, 4]
123 + 1 = 124.
Example 2
Input: digits = [1, 2, 9]
Output: [1, 3, 0]
The last digit 9 becomes 0 and carries 1 into the next digit, so 129 + 1 = 130.
Example 3
Input: digits = [9, 9, 9]
Output: [1, 0, 0, 0]
Every digit carries, so the answer has one more digit than the input: 999 + 1 = 1000.
Constraints
- 1 <= len(digits) <= 1000
- 0 <= digits[i] <= 9
- digits has no leading zero, unless the number is zero itself, which is [0].