Plus One
EasyMath & Geometry
You are given an integer array digits that represents a non-negative integer, one decimal digit per element, with the most significant digit first. The number has no leading zeros, except for the number 0 itself ([0]).
Add one to the number and return the result in the same format: a list of digits, most significant first, with no leading zeros.
Example 1
- Input:
- digits = [1,2,9]
- Output:
- [1,3,0]
- Explanation
- 129 + 1 = 130. The last digit 9 becomes 0 and carries 1 into the 2.
Example 2
- Input:
- digits = [9,9]
- Output:
- [1,0,0]
- Explanation
- 99 + 1 = 100. Every digit carries, so the answer needs one more digit than the input.
Example 3
- Input:
- digits = [0]
- Output:
- [1]
- Explanation
- 0 + 1 = 1.
Constraints
1 <= digits.length <= 1000 <= digits[i] <= 9digitshas no leading zeros unless it is[0]