# Leetcode 66. Plus One 給定一個非空之數字數組,該數字為一個非負整數,將該數字加1。 這個數組的存儲方式為如下: 假如數字為587則如下圖 |索引| 0 | 1 | 2 | |---|---|---|---| |數值| 5 | 8 | 7 | ## 想法: 從index的最大開始對數值加1,處理進位後的狀況以及999的進位後超過原本長度的狀況。 程式碼: ``` def plusOne(self, digits: List[int]) -> List[int]: index = len(digits)-1 digits[index]+=1 while(index>=0 and digits[index]>=10): if(index==0): digits[index]-=10 digits.insert(0,1) else: digits[index]-=10 digits[index-1]+=1 index-=1 return digits ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up