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不重复,且每一个 ...
随机推荐
- 微信小程序简单的数据表格及查询功能
简介: 此项目是一个前后端分离的小demo, 开发工具:idea+微信小程序开发工具 前端:界面布局样式和js的跳转 后端:依靠SpringBoot的业务逻辑层 项目的码云地址: https://gi ...
- Python常见数据类型及操作
基础数据类型 什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但计算机并不能,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’ ...
- 菜鸟之路——Linux基础::计算机网络基础,Linux常用系统命令,Linux用户与组权限
最近又重新安排了一下我的计划.准备跟着老男孩的教程继续学习,感觉这一套教程讲的很全面,很详细.比我上一套机器学习好的多了. 他的第一阶段是Python基础,第二阶段是高等数学基础,主要将机器学习和深度 ...
- JavaScript: 理解对象
ECMA-262 把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.” 严格来讲,这就相当于说对象是一组没有特定顺序的值.对象的每个属性或者方法都有一个名字,而每个名字都映射到一个值 ...
- 微信小程序--问题汇总及详解之清空电话号码
wxml: <view class="btns" wx:for="{{phoneList}}" wx:key="id"> < ...
- POJ3678 Katu Puzzle 【2-sat】
题目 Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean ...
- oracle存储过程、声明变量、for循环
oracle存储过程.声明变量.for循环 1.创建存储过程 create or replace procedure test(var_name_1 in type,var_name_2 out t ...
- EPPlus(SQL导成Excel)
使用Epplus方法把sql数据库中表的数据导出到excel中去: 需要使用EPPlus.dll引用. using System.IO; using OfficeOpenXml; public sta ...
- 小M的作物 最大权闭合子图
题目大意 bzoj 3438 两个田\(A,B\) \(n\le 1000\)种作物的种子 第\(i\)个种子,种\(A\)价值\(a[i]\),种\(B\)价值\(b[i]\) 再给出\(m\)个子 ...
- 用email实现邮件模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...