26 lines
983 B
Python
26 lines
983 B
Python
# 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.
|
|
|
|
# increment by one the last index of the array.
|
|
# iterate backwards, from the last index until index 0,
|
|
# if the index as a value of 10, set it to 0;
|
|
# if this is the index 0, insert the value 1 at index 1,
|
|
# otherwise increment the previous index by 1
|
|
# (we are running backwards in the array)
|
|
|
|
|
|
def plusOne(digits: list[int]) -> list[int]:
|
|
digits[-1] += 1
|
|
for n_digit in range(len(digits) - 1, -1, -1):
|
|
if digits[n_digit] == 10:
|
|
digits[n_digit] = 0
|
|
if n_digit == 0:
|
|
digits.insert(0, 1)
|
|
else:
|
|
digits[n_digit - 1] += 1
|
|
return digits
|