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 <= 1000 <= digits[i] <= 9digits 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:
Imagine you have a long sequence of digits written on paper representing a giant number, and you want to increase its value by one. The brute force strategy involves translating the paper digits into a standard number, adding one to it using normal math, and then breaking that new total back down into individual digits.
Here's how the algorithm would work step-by-step:
def plus_one(digits_sequence):
# Convert individual digits into a single large integer for standard arithmetic addition
combined_number = 0
for current_digit in digits_sequence:
combined_number = combined_number * 10 + current_digit
# Increment the numerical value by one to fulfill the core requirement of the problem
incremented_total = combined_number + 1
# Convert the incremented total back into a string representation to isolate each digit
total_as_string = str(incremented_total)
# Extract each character from the string and convert it back into an integer digit
result_digits = []
for character_digit in total_as_string:
result_digits.append(int(character_digit))
return result_digitsImagine you are writing down a number digit by digit on paper and you want to add one to the entire number. The most efficient strategy is to start at the very last digit on the right, just like you learned in school.
Here's how the algorithm would work step-by-step:
class Solution:
def plusOne(self, digits_array: list[int]) -> list[int]:
digit_index = len(digits_array) - 1
while digit_index >= 0:
# Digits below nine can absorb the increment without triggering a carry operation.
if digits_array[digit_index] < 9:
digits_array[digit_index] += 1
return digits_array
# Nines roll over to zero, requiring the carry to propagate leftward.
digits_array[digit_index] = 0
digit_index -= 1
# All nines require a leading one because the number length expands by one.
return [1] + digits_array| Case | How to Handle |
|---|---|
| Single digit array representing zero | The loop processes the single zero, changes it to one, and returns the array correctly. |
| Array with all nines such as 999 | Every digit becomes zero during the carry propagation, and a new array of size n plus one with a leading one is created and returned. |
| Array ending in a zero such as 120 | The loop encounters zero at the least significant position, increments it to one, and terminates early without triggering further carries. |
| Large scale inputs with thousands of digits | The algorithm operates in O(n) time complexity and uses O(1) extra space in place, scaling efficiently for massive arrays. |
| Language-specific integer overflow on conversion | By manipulating the array elements directly as digits instead of parsing the entire array into a primitive integer type, we completely avoid overflow issues. |