单词拆分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. Delphi 鼠标的编程

  2. poj 1007 DNA sorting (qsort)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95209   Accepted: 38311 Des ...

  3. VMWare中CentOS7 设置固定IP且能够访问外网

    最近搭建kubernetes集群环境时遇到一个问题,CentOS7在重启后IP发生变化导致集群中etcd服务无法启动后集群环境变得不可用,针对这种情况,必须要对CentOS7设置固定IP且可以访问外网 ...

  4. Golang中的匿名函数(闭包)

    GO语言的匿名函数就是闭包,以下是<GO语言编程>中对闭包的解释 基本概念闭包是可以包含自由(未绑定到特定对象)变量的代码块,这些变量不在这个代码块内或者任何全局上下文中定义,而是在定义代 ...

  5. 一跃进入C大门

    相对跳转:b,bl 绝对跳转:直接给PC指针赋值

  6. Go语言标准库之fmt.Scan

    Go语言fmt.Scan使用指南 本文介绍了Go语言中fmt包中从标准输入获取数据的的Scan系列函数.从io.Reader中获取数据的Fscan系列函数以及从字符串中获取数据的Sscan系列函数的用 ...

  7. Charles中使用Map Local提高测试效率

    书接上回,上次说到Charles中可以使用修改返回值来模拟接口返回,这次我们来说一下Charles中另外一个强大的功能. 我们用手机连接Charles,具体可以参考上一篇<借助Charles来测 ...

  8. Java I/O(三)各种Reader和Writer读写器、RandomAccessFile随机访问文件、序列化

    2019 01/01 八.Reader和Writer读写器 前面讲的输入输出流的基本单位都是字节,因此可以称为“字节流”,读写器是以字符为基本单位,可以称为“字符流”.它们的使用方法非常相似,因此我考 ...

  9. 【JZOJ5434】【NOIP2017提高A组集训10.30】Matrix

    题目 分析 假设答案为ans, 发现\[k=\sum_{i=1}^{min(n,k)}\lfloor \dfrac{ans}{i} \rfloor\] 于是可以对ans进行二分, 用分块来求出上面的式 ...

  10. 【leetcode】1254. Number of Closed Islands

    题目如下: Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally ...