From ca02eb659f3db6dfa49823d7c2ee199b8f9ea09b Mon Sep 17 00:00:00 2001 From: Luis Rodrigues Date: Wed, 12 Feb 2025 17:50:56 +0000 Subject: [PATCH] exercicio 66 resolvido em python --- 66-plus_one/python/66.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 66-plus_one/python/66.py diff --git a/66-plus_one/python/66.py b/66-plus_one/python/66.py new file mode 100644 index 0000000..02058de --- /dev/null +++ b/66-plus_one/python/66.py @@ -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