finish 66

This commit is contained in:
sangge 2024-03-09 04:34:38 +08:00
parent 211fcd1dfa
commit 8d26ef13f2

22
leetcode/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.
"""
from typing import List
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
n = len(digits)
# Start from the rightmost digit
for i in range(n - 1, -1, -1):
# If the current digit is less than 9, just increment it by 1 and return
if digits[i] < 9:
digits[i] += 1
return digits
# Otherwise, set the current digit to 0 and continue with the next digit
else:
digits[i] = 0
# If we reach here, it means all digits were 9, so we need to add a new leading 1
digits.insert(0, 1)
return digits