126. Leetcode 剑指 Offer 46. 把数字翻译成字符串 (动态规划- 字符串系列)
生活随笔
收集整理的這篇文章主要介紹了
126. Leetcode 剑指 Offer 46. 把数字翻译成字符串 (动态规划- 字符串系列)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Solution:def translateNum(self, num: int) -> int:nums = str(num)n = len(nums)#異常情況if n < 2:return 1# dp[i] 表示以第i個位置結尾的數字翻譯的方法數dp = [1 for _ in range(n)]for i in range(1, n):if nums[i-1] == '1':dp[i] = dp[i-1] + dp[i-2]elif nums[i-1] =='2' and nums[i] < '6':dp[i] = dp[i-1] + dp[i-2]else:dp[i] = dp[i-1]return dp[-1]
?
總結
以上是生活随笔為你收集整理的126. Leetcode 剑指 Offer 46. 把数字翻译成字符串 (动态规划- 字符串系列)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 125. Leetcode 91. 解码
- 下一篇: 127. Leetcode 242. 有