Increment a non-negative integer represented as an array of digits and return the updated digit array.
You are given a non-negative integer represented as an array of decimal digits, where the most significant digit comes first. Add one to the integer and return the resulting array of digits.
The returned array should not contain leading zeros unless the number itself is zero.
digits, where each element is an integer from 0 to 9.digits + 1.1 <= digits.lengthdigits[i] is in the range [0, 9]digits represents a valid non-negative integer without leading zeros, except possibly when the number is zero.Example 1
Input
digits = [1,2,3]
Output
[1,2,4]
Explanation
123 + 1 = 124.
Example 2
Input
digits = [4,3,2,1]
Output
[4,3,2,2]
Explanation
4321 + 1 = 4322.
Example 3
Input
digits = [9]
Output
[1,0]
Explanation
9 + 1 = 10.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.