思路:

直接爆搜会超时,需要使用记忆化搜索。使用map把已经计算过的情况记录下来,避免重复计算。

实现:

 class Solution
{
public:
vector<string> wordBreak(string s, vector<string>& wordDict)
{
unordered_map<int, vector<string>> dp;
return dfs(s, , wordDict, dp);
}
vector<string> dfs(string s, int cur, vector<string>& wordDict, unordered_map<int, vector<string>>& dp)
{
if (cur >= s.length())
{
vector<string> v;
v.push_back("");
return v;
}
if (dp.count(cur)) return dp[cur];
vector<string> ans;
for (auto it: wordDict)
{
int l = it.length();
if (cur + l <= s.length() && s.substr(cur, l) == it)
{
if (cur + l == s.length())
ans.push_back(it);
else
{
vector<string> tmp = dfs(s, cur + l, wordDict, dp);
for (auto it1: tmp)
{
ans.push_back(it + " " + it1);
}
}
}
}
return dp[cur] = ans;
}
};

leetcode140 Word Break II的更多相关文章

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

  2. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  3. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

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

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

  6. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  7. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  8. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

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

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

随机推荐

  1. jquery跨域3

    这两天用 Jquery 跨域取数据的时候,经常碰到 invalid label 这个错误,十分的郁闷,老是取不到服务器端发送回来的 json 值, 一般跨域用到的两个方法为:$.ajax 和$.get ...

  2. 「LOJ#103」子串查找 (Hash

    题目描述 这是一道模板题. 给定一个字符串 A A A 和一个字符串 B B B,求 B B B 在 A A A 中的出现次数.AAA 和 BBB 中的字符均为英语大写字母或小写字母. A A A 中 ...

  3. Microsoft Speech SDK开发包 使用

    下载开发包.我们首先从微软的官网上面下载开发包,下载地址如下: http://www.microsoft.com/en-us/download/details.aspx?id=10121我们主要下载三 ...

  4. 五、hibernate在myeclipse中生成实体和映射

  5. div显示2列

    #wdjContainer{ border:1px solid green; margin:10px auto; width:500px; height:300px; line-height:30px ...

  6. 28.Docker介绍与目录

    快速的部署和启动 docker的启动是毫秒级的.一分钟可移动几百个上千个docker的容器 docker和虚拟机的区别 虚拟机在里面独立运行完整的操作系统.资源上时间上都需要多. docker容器级别 ...

  7. jQuery 生成随机字符

    //获取长度为len的随机字母 //获取长度为len的随机字母 function zimu(len){ len = len || 1; var $chars = 'ABCDEFGHIJKLMNOPQR ...

  8. 如何实现Vue已经弃用的$dispatch和$broadcast方法?

    对于父子(含跨级)传递数据的通信方式,Vue.js 并没有提供原生的 API 来支持,而是推荐使用大型数据状态管理工具 Vuex,但 Vuex 对于小型项目来说用起来真的很麻烦. 在 Vue.js 1 ...

  9. Swift 数组,字典,结构体,枚举

    1.数组 let types = ["none","warning","error"]//省略类型的数组声明 var menbers = [ ...

  10. Codeforces482B【线段树构造】

    题意: 有M个限制,每个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是否有满足的数列. 思路: 看到大牛说是线段树,线段树对于区间操作,印象中乘啊,+啊,-啊都不错,但是并没有就是 ...