[Leetcode] 66. Plus One

閱讀時間約 3 分鐘

題目:66. Plus One


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.


Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].

Example 3:

Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].


1.


class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
# 將字串轉數字,再+1
num = int("".join(str(i) for i in digits )) + 1
ans = []
# 將num%10的餘數append到ans
while num != 0:
ans.append(num % 10)
num //= 10
#最後的ans會是倒序的數字,會以要將ans做反轉
return reversed(ans)






7會員
47內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
留言0
查看全部
發表第一個留言支持創作者!
Youna's Devlog 的其他內容
[Leetcode] 15. 3Sum
閱讀時間約 5 分鐘
[Leetcode] 13. Roman to Integer
閱讀時間約 5 分鐘
[Leetcode] 14. Longest Common Prefix
閱讀時間約 2 分鐘
[Leetcode] 9. Palindrome Number
閱讀時間約 1 分鐘
你可能也想看
迎新活動「方格新手村」:新格友註冊加入方格子,知名日料吃到飽餐券送給你! 👉 還不是 vocus 的會員嗎?點此註冊,參與新手村活動 👈 近期站上也出現了不少新格友,為了歡迎各位的加入,「方格新手村」隨之登場! 即日起,只要是新註冊帳號於活動期間內發佈 3 則文章,就有機會抽獎獲得知名日料吃到飽餐券。原格友也可以一起同樂,我們準備了小任
Thumbnail
2024-06-21
103
什麼人適合國泰世華 CUBE App ?整理5大推薦原因你覺得哪間網銀的 App 最好用? 近幾年,我為了拿到很多家銀行的回饋,辦了很多家銀行的帳戶,數一數至少也有十幾間! 比較下來,國泰世華 CUBE App ,是我用起來最順手的 👍 國泰究竟贏在哪裡呢?讓我整理 5 個原因給大家~
Thumbnail
2024-07-05
66
川普當選,對台股是利多還是利空?川普在槍擊事件中所表現出來的英勇形象,讓他贏得美國總統大選幾乎已成定局。沒想到他隨口的一句話「台灣搶走美國的晶片生意,所以要付保護費」,就讓台積電在短短三天跌掉超過100 元,台股也跌掉1100點以上。台積電、台股會就此一路下跌嗎?未來該如何因應?  
Thumbnail
2024-07-20
80
【LeetCode】19.Remove Nth Node From End of ListInput: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Thumbnail
發佈在
Err500
2021-10-09
2
【LeetCode】876. Middle of the Linked ListInput: head = [1,2,3,4,5] Output: [3,4,5] 單看列表只是要找中間值,不過給定的資料結構不是陣列,而是鏈結串列。
Thumbnail
發佈在
Err500
2021-10-09
0
【LeetCode】189. Rotate Array之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Thumbnail
發佈在
Err500
2021-10-08
2
【LeetCode】557. Reverse Words in a String III今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Thumbnail
發佈在
Err500
2021-10-08
1
【LeetCode】344. Reverse String今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
Thumbnail
發佈在
Err500
2021-10-08
0
【LeetCode】167. Two Sum II 題目如下: Input: numbers = [2,7,11,15], target = 9 Output: [1,2]
Thumbnail
發佈在
Err500
2021-10-07
2
【LeetCode】283. Move Zeroes題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
Thumbnail
發佈在
Err500
2021-10-07
0
【LeetCode】977. Squares of a Sorted Array比今天的題目示例如下: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] 簡單來說,要的輸出結果是把陣列內每個數字取平方後,對陣列做排序。
Thumbnail
發佈在
Err500
2021-10-06
2
【LeetCode】704. Binary SearchLeetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
Thumbnail
發佈在
Err500
2021-10-05
3
【LeetCode】20. Valid Parentheses 前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。
Thumbnail
發佈在
Err500
2021-10-04
3
迎新活動「方格新手村」:新格友註冊加入方格子,知名日料吃到飽餐券送給你! 👉 還不是 vocus 的會員嗎?點此註冊,參與新手村活動 👈 近期站上也出現了不少新格友,為了歡迎各位的加入,「方格新手村」隨之登場! 即日起,只要是新註冊帳號於活動期間內發佈 3 則文章,就有機會抽獎獲得知名日料吃到飽餐券。原格友也可以一起同樂,我們準備了小任
Thumbnail
2024-06-21
103
什麼人適合國泰世華 CUBE App ?整理5大推薦原因你覺得哪間網銀的 App 最好用? 近幾年,我為了拿到很多家銀行的回饋,辦了很多家銀行的帳戶,數一數至少也有十幾間! 比較下來,國泰世華 CUBE App ,是我用起來最順手的 👍 國泰究竟贏在哪裡呢?讓我整理 5 個原因給大家~
Thumbnail
2024-07-05
66
川普當選,對台股是利多還是利空?川普在槍擊事件中所表現出來的英勇形象,讓他贏得美國總統大選幾乎已成定局。沒想到他隨口的一句話「台灣搶走美國的晶片生意,所以要付保護費」,就讓台積電在短短三天跌掉超過100 元,台股也跌掉1100點以上。台積電、台股會就此一路下跌嗎?未來該如何因應?  
Thumbnail
2024-07-20
80
【LeetCode】19.Remove Nth Node From End of ListInput: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Thumbnail
發佈在
Err500
2021-10-09
2
【LeetCode】876. Middle of the Linked ListInput: head = [1,2,3,4,5] Output: [3,4,5] 單看列表只是要找中間值,不過給定的資料結構不是陣列,而是鏈結串列。
Thumbnail
發佈在
Err500
2021-10-09
0
【LeetCode】189. Rotate Array之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Thumbnail
發佈在
Err500
2021-10-08
2
【LeetCode】557. Reverse Words in a String III今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Thumbnail
發佈在
Err500
2021-10-08
1
【LeetCode】344. Reverse String今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
Thumbnail
發佈在
Err500
2021-10-08
0
【LeetCode】167. Two Sum II 題目如下: Input: numbers = [2,7,11,15], target = 9 Output: [1,2]
Thumbnail
發佈在
Err500
2021-10-07
2
【LeetCode】283. Move Zeroes題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
Thumbnail
發佈在
Err500
2021-10-07
0
【LeetCode】977. Squares of a Sorted Array比今天的題目示例如下: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] 簡單來說,要的輸出結果是把陣列內每個數字取平方後,對陣列做排序。
Thumbnail
發佈在
Err500
2021-10-06
2
【LeetCode】704. Binary SearchLeetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
Thumbnail
發佈在
Err500
2021-10-05
3
【LeetCode】20. Valid Parentheses 前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。
Thumbnail
發佈在
Err500
2021-10-04
3