leetcode 140 单词拆分2 word break II

单词拆分2,递归+dp,
需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下
class Solution {
public:
//定义一个子串和子串拆分(如果有的话)的映射
unordered_map<string,vector<string>>m;
vector<string> wordBreak(string s, vector<string>& wordDict) {
if(m.count(s)) return m[s];//当映射的子串有s说明已经判断完了返回拆分m[s]
if(s.empty()) return {""};//假如s是空的返回空串
vector<string> res;
for(string word: wordDict){
if(s.substr(,word.size())!=word) continue;//在s开头找到字典中的词;
vector<string> rem=wordBreak(s.substr(word.size()),wordDict);//rem是剩下的子串拆分,递归调用wordBreak
for(string str:rem){
res.push_back(word+(str.empty() ? "" : " ")+str);//str非空时,前面加个空格后面再加单词;
}
}
return m[s]=res;
}
};
参考:http://www.cnblogs.com/grandyang/p/4576240.html
leetcode 140 单词拆分2 word break II的更多相关文章
- leetcode 139 单词拆分(word break)
一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
- [LeetCode] 140. 单词拆分 II
题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...
- Java实现 LeetCode 140 单词拆分II
class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...
- 单词拆分 I · Word Break
[抄题]: 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. s = "lintcode" dict = ["lint" ...
- LeetCode 139. 单词拆分(Word Break)
139. 单词拆分 139. Word Break
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 【LeetCode】140. Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
随机推荐
- mac系统下Eclipse + pydev配置python Interpreter
mac系统下Eclipse + pydev配置python Interpreter 之前都在windows下使用Eclipse + pydev 进行开发,未发现什么异常,最近对wxpy.itcha ...
- 19、Firewalld防火墙
安全的考虑方向: 安全框架 OSI七层模型 硬件 机架上锁(机柜) 温度 硬件检查 网络 iptables/firewalld 仅允许公司的IP地址能连接服务器的22端口 公有云使用 安全组 系统 没 ...
- 21、Nginx 常见问题
1.多个server_name容易产生冲突,会按照如下顺序匹配 1.首先选择所有的字符串完全匹配的server_name.(完全匹配) 2.选择通配符在前面的server_name,如*.bgx.co ...
- Win10下注册APlayer组件的正确姿势
1. 官网下载SDK 和 解码器 APlayer媒体播放引擎 2.解压SDK和解码器,把解码器codecs文件夹内所有文件复制到SDK文件夹内的bin\codecs目录里面 3.使用管理员权限打开CM ...
- Automatches
import os def combine(ArrayList,count): ArrayList=list(ArrayList) newArrayList=[] for i in range(0,A ...
- EF Core命令
新建 Add-Migration init Update-Database init 修改model后,执行迁移的命令 更新数据库 每次更新都要{update}修改 Add-Migration {up ...
- 表空间及段区块的一些sql语句和视图
查询段情况的语句 select segment_name,blocks,extents,bytes,segment_type,tablespace_namefrom dba_segments wher ...
- qtmaind.lib(qtmain_win.obj) : error LNK2019: 无法解析的外部符号 __imp_CommandLineToArgvW,该符号在函数 WinMain 中被引用
报错:qtmaind.lib(qtmain_win.obj) : error LNK2019: 无法解析的外部符号 __imp_CommandLineToArgvW,该符号在函数 WinMain 中被 ...
- (转)HTTP请求报文和HTTP响应报文
原地址:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/28/2612910.html HTTP报文是面向文本的,报文中的每一个字段都是一些ASC ...
- jquery pageY属性 语法
jquery pageY属性 语法 作用:pageY() 属性是鼠标指针的位置,相对于文档的上边缘.直线模组 语法:event.page 参数: 参数 描述 event 必需.规定要使用的事件 ...