exercicio 66 resolvido em python

This commit is contained in:
2025-02-12 17:50:56 +00:00
parent 26b71d6d46
commit ca02eb659f

22
66-plus_one/python/66.py Normal file
View File

@ -0,0 +1,22 @@
# 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.
# só é preciso incrementar o valor no ultimo indice
# -> mas e se o numero for '129'? o resultado correcto será '130' e não '120'
#
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