You are given a large integer represented as an integer array digits
, where each digits[i]
is the ith
digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0
's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].
Example 2:
Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].
Example 3:
Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
Constraints:
1 <= digits.length <= 100
0 <= digits[i] <= 9
digits
does not contain any leading 0
's.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given a number represented as a list of digits and need to add one to it. The most straightforward way is to simulate adding one the way we learned in grade school, handling any carry-overs as they arise.
Here's how the algorithm would work step-by-step:
def plus_one_brute_force(digits):
number_of_digits = len(digits)
carry = 0
digits[number_of_digits - 1] += 1
for i in range(number_of_digits - 1, -1, -1):
digits[i] += carry
carry = 0
# If the digit becomes 10, reset to zero and carry over
if digits[i] == 10:
digits[i] = 0
carry = 1
# If we're at the beginning and still have a carry, add a new digit
if i == 0 and carry == 1:
digits.insert(0, 1)
else:
break
return digits
The key is to simulate how you would add one to a number written on paper, starting from the rightmost digit. We need to handle cases where adding one results in a carry-over, which might propagate through the entire number.
Here's how the algorithm would work step-by-step:
def plus_one(digits):
number_of_digits = len(digits)
carry = 1
for i in range(number_of_digits - 1, -1, -1):
digit_value = digits[i] + carry
# If sum is 10, reset digit and carry over
if digit_value == 10:
digits[i] = 0
carry = 1
# Otherwise, update digit and clear carry
else:
digits[i] = digit_value
carry = 0
break
# If there's still a carry after processing all digits
if carry == 1:
digits.insert(0, 1)
return digits
Case | How to Handle |
---|---|
Null or empty input array | Return an array containing only the digit '1' representing the increment of zero by one or throw an IllegalArgumentException. |
Single digit array with value 9 | The result should be [1, 0], requiring a new array allocation and carrying over. |
Array with all 9s (e.g., [9, 9, 9]) | The result should be [1, 0, 0, 0], requiring a new array allocation and handling multiple carries. |
Large input array (performance consideration) | The solution should iterate from right to left, avoiding unnecessary operations on earlier digits if no carry is needed. |
Array with leading zeros (e.g., [0, 0, 1, 2, 3]) - although technically invalid input according to the problem description | The code should operate correctly on this input, treating it as 123 and producing [1, 2, 4], though input validation could also strip leading zeros initially. |
Array contains a non-digit number (e.g. [1, 2, -3]) | Throw an IllegalArgumentException since the input must contain only digits. |
Array with a digit greater than 9 (e.g. [1, 2, 10]) | Throw an IllegalArgumentException since the input digits cannot exceed 9. |
Input represents the maximum possible integer value that can be stored | The algorithm inherently handles large numbers represented as arrays of digits, as it avoids direct integer manipulation, thereby dodging overflow issues. |