From 8d26ef13f237c626f4af60031ad3f32fcbf5e844 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 9 Mar 2024 04:34:38 +0800 Subject: [PATCH] finish 66 --- leetcode/66.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 leetcode/66.py diff --git a/leetcode/66.py b/leetcode/66.py new file mode 100644 index 0000000..b57fec6 --- /dev/null +++ b/leetcode/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. +""" + +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