欢迎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. Leetcode 636.函数的独占时间

    函数的独占时间 给出一个非抢占单线程CPU的 n 个函数运行日志,找到函数的独占时间. 每个函数都有一个唯一的 Id,从 0 到 n-1,函数可能会递归调用或者被其他函数调用. 日志是具有以下格式的字 ...

  2. python 文件(file)操作

    操作文件的一般流程有: 打开文件.文件处理.关闭文件 开开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内容:] a,追加模式.[不可读: 不存在则创建:存在 ...

  3. LINUX 常用指令学习

    目录 0 查找find 1 别名alias 2 变量的设置 3 常用的系统变量 4 通配符及组合按键 5 指令之间的分隔符(;&||) 6 输出重定向(>,>>,1>, ...

  4. SQL Server2012使用导入和导出向导时,用sql语句作为数据源,出现数据源类型会变成202或者203

    用MS SqlServer2012进行数据导出时,使用的查询语句导出,但是出现了错误: “发现 xx个未知的列类型转换您只能保存此包“ 点击列查看详细错误信息时,可以看到: [源信息]源位置: 192 ...

  5. (转)iOS-蓝牙学习资源博文收集

    ios蓝牙开发(一)蓝牙相关基础知识 ios蓝牙开发(二)蓝牙中心模式的ios代码实现 ios蓝牙开发(三)app作为外设被连接的实现 ios蓝牙开发(四)BabyBluetooth蓝牙库介绍 暂未完 ...

  6. 编程风格——UNIX 高手的 10 个习惯

    引言 当您经常使用某个系统时,往往会陷入某种固定的使用模式.有时,您没有养成以尽可能最好的方式做事的习惯.有时,您的不良习惯甚至会导致出现混乱.纠正此类缺点的最佳方法之一,就是有意识地采用抵制这些坏习 ...

  7. 【02】react 之 jsx

    React与ReactDOM是react中核心对象,React为核心功能,ReactDOM提供对DOM的操作,以前的老版本中只有React没有ReactDOM,新版本中分离出ReactDOM提供两种渲 ...

  8. 【06】next() 伪函数

    串行,第一个完成后,去执行第二个第二个异步任务,使用next()尾函数.首先我么想完成三个任务,task1,task2,task3,如图: 实现方式1: var fs = require(" ...

  9. 负载平衡(cogs 741)

    «问题描述:G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运.«编程任务:对于给定的n 个环 ...

  10. GridView 动态绑定控件 OnRowCommand事件触发

    主题:GridView动态生成的控件不能触发OnRowCommand事件,且点击控件按钮后,控件的值会消失. 案例, 由于公司需要绑定的数据列顺序是动态生成的,且有的数据列需要绑定Button控件.所 ...