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不重复,且每一个 ...
随机推荐
- VMware Fusion Pro安装Ubuntu 18.04.1
- c++中读取文件最快的方法
https://www.byvoid.com/blog/fast-readfile 可以看看了.
- Android简单的BaseExpandableList使用
1.Activity package com.example.administrator.mystudent.ExpandableListView; import android.app.Expand ...
- iOS开发UI篇—CALayer
一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能显示在屏幕上,完全 ...
- 关于javascript的"+="连接符
今天在读<javascript Dom 编程艺术>的时候,看到了自己感觉陌生的+=连接符(小白一枚,各位勿耻笑) "+="连接符,可以看成完成一次“加法和赋值”(或者“ ...
- 关于Delphi cxGrid主从表中从表只能编辑第一条记录的问题
在Delphi cxGrid主从表中从表只能编辑第一条记录,这个问题是由于设置主从关联字段错误造成的. 从表DBtableView2的keyfieldnames,DetailKeyFieldNames ...
- C# 获取NTP远程同步时间
收到一个需要定时同步远程服务器的需求,用C# 实现 网上搜索到解决方案,代码如下: 获取远程时间 参数配置:"NTPServer" 远程时间服务器地址 获取远程服务器时间代码: ...
- Js 中 == 与 === 的区别
1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...
- ClassLoader 提供了两个方法用于从装载的类路径中取得资源:
转:http://cheneyph.iteye.com/blog/831721 ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource ( ...
- wap开发杂项1
原文发布时间为:2010-08-31 -- 来源于本人的百度文章 [由搬家工具导入] wap开发全程记忆[CLQ原创 持续更新] 1. 据说手机wap最好都是utf-8字符集,不过gb2312对移动手 ...