欢迎fork and star:Nowcoder-Repository-github

140. Word Break II

题目:

 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"]. A solution is ["cats and dog", "cat sand dog"]. UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

解析

  • unordered_set& dict办版本
	//运行时间:4ms
//占用内存:508k class Solution { vector<string> combine(string word, vector<string> prev){
for (int i = 0; i < prev.size(); ++i){
prev[i] += " " + word;
}
return prev;
} public:
vector<string> wordBreak(string s, unordered_set<string>& dict) { vector<string> result;
if (dict.count(s)){ //a whole string is a word
result.push_back(s);
}
for (int i = 1; i < s.size(); ++i){
string word = s.substr(i);
if (dict.count(word)){
string rem = s.substr(0, i);
vector<string> prev = combine(word, wordBreak(rem, dict));
result.insert(result.end(), prev.begin(), prev.end());
}
} reverse(result.begin(), result.end());
return result;
}
};
  • 暴力超时
namespace test
{
vector<string> wordBreak(string s, unordered_set<string> &dict) {
//暴力搜索,不能ac,复杂度超了。
vector<string> res;
int size = s.length();
for (int i = 0; i < size; i++){
string tmp = s.substr(0, i + 1);
if (dict.count(tmp))
findNext(res, s, dict, tmp, i + 1);
}
return res;
} void findNext(vector<string> & res, string s, unordered_set<string> &dict, string tmp, int i){
int size = s.length();
if (i >= size){
res.push_back(tmp);
return;
}
for (int j = 1; j <= size - i; ++j){
string now = s.substr(i, j);
if (dict.count(now)){
findNext(res, s, dict, tmp + ' ' + now, i + j);
}
}
}
}

题目来源

140. Word Break II(hard)的更多相关文章

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

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

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

  3. 140. Word Break II

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

  4. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  5. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  6. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  7. leetcode 140. Word Break II ----- java

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. Java for LeetCode 140 Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

随机推荐

  1. android TranslateAnimation动画执行时的坐标获取。

    android 的Tween动画并不会改变控件的属性值,比如以下测试片段: 定义一个从屏幕右边进入,滚动到屏幕左边消失的一个TranslateAnimation动画: <?xml version ...

  2. iptables端口转发命令

    需求很简单,把本地81端口映射到8080端口上 1. 所有的81请求转发到了8080上.   1 # iptables -t nat -A PREROUTING -p tcp --dport 81 - ...

  3. BZOJ 4826 [Hnoi2017]影魔 ——扫描线 单调栈

    首先用单调栈和扫描线处理出每一个数左面最近的比他大的数在$l[i]$,右面最近的比他大的数$r[i]$. 然后就可以考虑每种贡献是在什么时候产生的. 1.$(l[i],r[i])$产生$p1$的贡献 ...

  4. linux系统——hosts文件修改

    1. 关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) Linux 的/etc/hosts是 ...

  5. 无序字母对 character

    无序字母对 character 题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入 ...

  6. Ionic2 调用自定义插件之研究

    cordova机制我在此就不提了,我们使用Typescript调用cordova plugin就如同调用第三方库是一个道理,那么这里就少不了书写declare文件,下面我就把几种封装调用的几种方式介绍 ...

  7. sql2008游标FORWARD_ONLY STATIC 的使用方式

    CREATE TABLE #xms_staff_department ( id int, name varchar(128), parent_id int, parent_path varchar(5 ...

  8. 洛谷 [P3623] 免费道路

    有 k 条特殊边的生成树 我们发现有一些边是必须的,如果把所有的水泥路都加入并查集,再枚举鹅卵石路,如果这条路能再次加入并查集,说明这条路是必须的 水泥路同样 这样就把必需边求出来了,剩下就可以随意加 ...

  9. UIImage与Base64相互转换

    UIImage与Base64相互转换 采用第三方类 Address:https://github.com/l4u/NSData-Base64/ 经测试好用. 2013-07-17

  10. .net连接mysql

    首先在官网下载,mysql-connect-net,用于使用mysql的驱动程序,我在下载mysql-connect-net.msi. installer后,执行安装程序的时候一直无法安装成功,最简单 ...