单词拆分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的更多相关文章

  1. leetcode 139 单词拆分(word break)

    一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...

  2. Java实现 LeetCode 140 单词拆分 II(二)

    140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...

  3. [LeetCode] 140. 单词拆分 II

    题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...

  4. Java实现 LeetCode 140 单词拆分II

    class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...

  5. 单词拆分 I · Word Break

    [抄题]: 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. s = "lintcode" dict = ["lint" ...

  6. LeetCode 139. 单词拆分(Word Break)

    139. 单词拆分 139. Word Break

  7. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. 韦东山嵌入式Linux学习笔记08--中断体系结构

    中断是什么? 举个栗子, 系统怎么知道你什么时候插入鼠标这个设备? 可以有两种处理方式: 1. 查询方式: 轮询去检测是否有设备插入; 2. 中断的方式 当鼠标插入这个事件发生时, 置位某个寄存器,告 ...

  2. 11条sql技巧

    1. 负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好习惯 可以优化为in查询: ...

  3. PAT Basic 1020 月饼 (25 分)

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  4. string::find_first_of

    string (1) size_t find_first_of (const string& str, size_t pos = 0) const noexcept; c-string (2) ...

  5. 2017 CVTE Windows开发一面 3.7

    下午3点接到了个广州打过来的电话,电话面试总体时间比较短,35分钟. 考察内容: 1.讲实习: 因人而异,将了之前公司做的项目,刚好和面的岗位匹配,面试官听完之后还不忘垂壁一下他们的产品. 2.C#事 ...

  6. Tomcat非root身份运行制作Linux系统服务管理

    理论知识怱略,马上开始实战 一.首先准备好tomcat 启动.关闭.重启Shell脚本: 以下Shell脚本主要修改值 tomcatPath:tomcat目录 runUser:以哪个身份运行 此处测试 ...

  7. win10 1903 更改文字大小

    标题栏 - 菜单 - 消息框 - 调色板标题11- 图标 - 工具提示 - Caption 标题 的 宽/高 - ; 14的宽高 - 菜单 的 宽/高 - ; 的宽高 -; 设置 注册表 HKEY_C ...

  8. 7、菜单栏、工具栏、状态栏、浮动窗口、TextEdit

    新建项目,基类选择QMainWindow,不勾选ui    mainwindow.cpp代码: #include "mainwindow.h" #include <QMenu ...

  9. 悲观锁,乐观锁以及MVCC

    在上文中,我们探讨了MySQL不同存储引擎中的各类锁,在这篇文章中我们将要讨论的是MySQL是如何实现并发控制的.并发问题有三种,分别为: 读-读,不存在任何问题 读-写,有隔离性问题,可能遇到脏读( ...

  10. ELEMENT-UI 封装el-table 局部刷新row

    //关于封装的el-table行数据更新后如何局部更新row row.status=status; this.$set(this.$refs.elTable.$data.tableData,index ...