You are given an integer array digits, where each digits[i] is the ith digit of a large integer. It is ordered from most significant to least significant digit, and it will not contain any leading zero.
Return the digits of the given integer after incrementing it by one.
Example 1:
Input: digits = [1,2,3,4]
Output: [1,2,3,5]Explanation 1234 + 1 = 1235.
Example 2:
Input: digits = [9,9,9]
Output: [1,0,0,0]Constraints:
1 <= digits.length <= 1000 <= digits[i] <= 9
You should aim for a solution with O(n) time and O(n) space, where n is the size of the input array.
There are two cases when adding 1 to a number. If the rightmost digit is less than 9, we simply increment it. Otherwise, we set it to 0 and apply the same process to the preceding digit.
We iterate through the given digits from right to left using index i. If the current digit is less than 9, we increment it and return the array. Otherwise, we set the digit to 0 and continue. If the loop completes without returning, we insert 1 at the beginning of the array and return it.
Before attempting this problem, you should be comfortable with:
We are given a number represented as an array of digits, and we need to add one to this number.
The challenge comes from handling the carry:
9, we can simply increment it.9, it becomes 0 and we need to carry +1 to the remaining digits.This recursive solution mirrors how addition works by hand:
[1]9:19):0plusOne on all digits except the last0 to the resultWe are given a number as an array of digits and need to add one to it.
The main idea is to simulate manual addition starting from the least significant digit (the last digit).
Since addition naturally moves from right to left, this solution:
one to represent the carry (initially 1)0This avoids recursion and handles all carry cases, including when the number consists entirely of 9s (like [9,9,9]).
one = 1 (this represents the +1 we want to add)i = 0 (index to traverse digits)digits array so the least significant digit comes first.one == 1):i is within the array:digits[i] == 9:digits[i] = 0 (carry continues)digits[i] by 1one = 0 (carry resolved)1 to the arrayone = 0idigits.We are given a number represented as an array of digits and need to add one to it.
The simplest way to do this is to simulate how addition works from right to left:
9, we can increment it and stop9, it becomes 0 and we carry +1 to the next digit on the leftIf we finish processing all digits and still have a carry, it means the number was something like [9, 9, 9], and we need to add a new leading 1.
n be the number of digits.i:digits[i] < 9:digits[i] by 1digits[i] == 9):digits[i] = 0 and continue to the next digit on the left9:[1] + digitsWhen the input is [9, 9, 9], every digit carries over and you need a new leading digit. Returning [0, 0, 0] instead of [1, 0, 0, 0] is a common mistake when the carry-out case is not explicitly handled.
Starting from index 0 and adding one there ignores how arithmetic carry propagates from the least significant digit. Always process from the last element toward the first to correctly simulate manual addition.