一维动态规划——字符串,可以使用dfs+cache,也可以改写为dp数组

91. 解码方法

难度中等

一条包含字母 A-Z 的消息通过以下映射进行了 编码 :

'A' -> "1"
'B' -> "2"
...
'Z' -> "26"

要 解码 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,"11106" 可以映射为:

  • "AAJF" ,将消息分组为 (1 1 10 6)
  • "KJF" ,将消息分组为 (11 10 6)

注意,消息不能分组为  (1 11 06) ,因为 "06" 不能映射为 "F" ,这是由于 "6" 和 "06" 在映射中并不等价。

给你一个只含数字的 非空 字符串 s ,请计算并返回 解码 方法的 总数 。

题目数据保证答案肯定是一个 32 位 的整数。

 

示例 1:

输入:s = "12"
输出:2
解释:它可以解码为 "AB"(1 2)或者 "L"(12)。

示例 2:

输入:s = "226"
输出:3
解释:它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。

示例 3:

输入:s = "06"
输出:0
解释:"06" 无法映射到 "F" ,因为存在前导零("6" 和 "06" 并不等价)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def numDecodings(self, s: str) -> int:
        n = len(s)       
        wordDict = {str(i) for i in range(1, 27)}       
        dp = [0] * (n+1)
        dp[0] = 1
 
        for i in range(1, n+1):
            if s[i-1] in wordDict:
                dp[i] = dp[i-1]
            if i >= 2 and s[i-2:i] in wordDict:
                dp[i] += dp[i-2]
 
        return dp[n]

 

使用dfs + cache也可以:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def numDecodings(self, s: str) -> int:
        n = len(s)       
        wordDict = {str(i) for i in range(1, 27)}
        self.cache = {n : 1}
 
        def dfs(index):
            if index in self.cache:
                return self.cache[index]
                         
            word = s[index]
            n1 = 0
            if word in wordDict:           
                n1 = dfs(index+1)
 
            n2 = 0
            if index + 1 < n:            
                word2 = s[index:index+2]
                if word2 in wordDict:
                    n2 = dfs(index+2)
             
            self.cache[index] = n1+n2
            return self.cache[index]
                 
        return dfs(index=0)

  

 

140. 单词拆分 II

难度困难

给定一个字符串 s 和一个字符串字典 wordDict ,在字符串 s 中增加空格来构建一个句子,使得句子中所有的单词都在词典中。以任意顺序 返回所有这些可能的句子。

注意:词典中的同一个单词可能在分段中被重复使用多次。

 

示例 1:

catsanddog
["cat","cats","and","sand","dog"]
["cats and dog","cat sand dog"]

示例 2:

输入:s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
输出:["pine apple pen apple","pineapple pen apple","pine applepen apple"]
解释: 注意你可以重复使用字典中的单词。

示例 3:

输入:s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
输出:[]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
        n = len(s)
        path = []
        wordDict = set(wordDict)
        def dfs(index):
            if index == n:
                self.ans.append(" ".join(path))
                return
 
            for i in range(index,  n):
                word = s[index:i+1]
                if word in wordDict:
                    path.append(word)
                    dfs(i+1)
                    path.pop()
             
        self.ans = []
        dfs(index=0)
        return self.ans

  

posted @   bonelee  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2022-01-30 检测Linux Rootkit入侵威胁——阿里云是基于行为特征如信号劫持或者文件隐藏,用户提权和网络隐藏,进程劫持等进行检测
2022-01-30 Linux下基于内存分析的Rootkit检测方法——传统方法还是检查已知Rootkit组件默认安装路径上是否存在相应文件,并比对文件签名(signature)。这种检测方式显然过于粗糙,对修改过的/新的Rootkit基本无能为力
2021-01-30 TrickBot 恶意木马软件
2018-01-30 tflearn中num_epoch含义就是针对所有样本的一次迭代
点击右上角即可分享
微信分享提示