Leetcode | Work Break I & II
Work Break I
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
DP很容易就出来了。possible[i]保存[0,i]是否可以被分割的结果。
possible[i] = true, 当存在possible[k] = true,且[k,i]是dict里的一个word时。否则possible[i] = false。
这种是自底而下的。
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
int n = s.length();
if (n == ) return true;
vector<bool> possible(n, false);
for (int i = ; i < n; ++i) {
for (int j = i; j >= ; --j) {
if ((j == || possible[j - ]) && dict.find(s.substr(j, i - j + )) != dict.end()) {
possible[i] = true;
break;
}
}
}
return possible[n - ];
}
};
算法的时间复杂度最坏情况是O(n^2),空间复杂度是O(n)。
网上也有人用前缀树(Trie树、字典树)实现的。私以为用前缀树还得先将dict里的所有word插进去,时间复杂度为O(n*l+s),l为word的最大长度,s为dict的大小。如果dict的大小比n大得多,那么整个开销也是不菲的。
只要稍微将上面的代码优化一下,先求出word的最大长度,那么时间复杂度也可以优化到O(n*l+s)。
Work Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
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"].
直接用回溯就可以了。自顶而下。
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> ret;
bt(s, dict, "", s.length(), ret);
return ret;
}
void bt(string &s, unordered_set<string> &dict, string str, int index, vector<string> &ret) {
if (index < ) {
ret.push_back(str.substr(, str.length() - ));
return;
}
for (int i = index; i >= ; --i) {
string tmp = s.substr(i, index - i + );
if (dict.find(tmp) != dict.end()) {
bt(s, dict, tmp + " " + str, i - , ret);
}
}
}
};
Leetcode | Work Break I & II的更多相关文章
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- [Git] Git 文件归档, include submodule
git archive命令,可以对任意提交对应的目录树建立归档. $ git archive -o latest.zip HEAD 基于最新提交建立归档文件latest.zip $ git ar ...
- FreeMarker备忘
以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: ,文本:直接输出的部分 ,注释:<#-- ... --& ...
- Linux查询网址
1.man查询手册 LINUX MAN PAGES ONLINE: http://man.he.net/ 2.编码规范 https://www.kernel.org/doc/Documentation ...
- Javascript操作剪切板数据(支持IE、Chrome、360、搜狗),亲测!
clipboarddata只能在IE浏览器中使用,在chrome下会提示对象未定义!以下的方法支持IE.Chrome.360.搜狗等浏览器,其它浏览器还未验证. <!DOCTYPE html&g ...
- oracle11g客户端 安装图解
软件位置:我的网盘 -- oracle空间 -- oracle工具 -- win64_11gR2_database_clint(客户端) -- 压缩软件包 先将下载下来的ZIP文件解压,并运行setu ...
- C# 读取本地图片 转存到其他盘符
UpFileContent upfile = new UpFileContent(); upfile.StationImageName = "123.png"; FileStrea ...
- MVC的处理过程
MVC的处理过程,首先控制器接受用户的请求,并决定应该调用哪个模型来进行处理,然后模型用业务逻辑来处理用户的请求并返回数据,最后控制器用相应的视图格式化模型返回的数据,并通过表示层呈现给用户.
- Mysql 对数字的格式化
format函数: 格式化浮点数 format(number, length); Formats the number X to a format like '#,###,###.##', r ...