140. Word Break II(hard)
欢迎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)的更多相关文章
- 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 ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- [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 ...
- 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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 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 ...
- 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 ...
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
随机推荐
- [python][django学习篇][6]操作数据库
查询(取)数据 >>> Category.objects.all() <QuerySet [<Category: Category object>]> > ...
- kvm配置虚拟机[待整理]
working note 4-12-2016 1,利用libvirt图形虚拟机管理工具virt-manager搭建虚拟机,通过存储池(storage pool )和卷(volume)存放虚拟机镜像(I ...
- java io 流 输入输出 大牛经典总结
在软件开发中,数据流和数据库操作占据了一个很重要的位置,所以,熟悉操作数据流和数据库,对于每一个开发者来说都是很重要的,今天就来总结一下I/O,数据库操作 一:从数据流开始 首先先有一个结构图看一下整 ...
- redhat linux 7.4关闭透明大页
每一步: 在GRUB_CMDLINE_LINUX加入选项 transparent_hugepage=never echo 'GRUB_CMDLINE_LINUX="transparent_h ...
- 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- 浅谈后缀自动机SAM
一下是蒟蒻的个人想法,并不很严谨,仅供参考,如有缺误,敬请提出 参考资料: 陈立杰原版课件 litble 某大神 某大神 其实课件讲得最详实了 有限状态自动机 我们要学后缀自动机,我们先来了解一下自动 ...
- java递归处理文件夹和文件
import java.io.File; /** * 文件综合使用示例 */ public class FileDelete { public static void main(String[] ar ...
- sys.modules[__name__]
A way to get a handle to the current running module in Python: import sys module = sys.modules[__nam ...
- linux之tee
tee命令 tee把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中 如果想看到输出的同时,把输出也同时拷入一个文件,这个命令很合适 格式:tee -a file -a 表示文件追加到末尾 ...
- Select语句执行顺序《转》
原文发布时间为:2010-10-12 -- 来源于本人的百度文章 [由搬家工具导入] 目的在于理解如何Select 【搜索所得】: 标准的 SQL 的解析顺序为:(1).FROM 子句, 组装来自不同 ...