leetcode-word break-ZZ
题目, 反正就是一个string,要不自己在字典里,要不切几刀,切出来的每个词都在字典里
———————————————————————————————————————————————————————-
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".
———————————————————————————————————————————————————————-
最笨的解法,DFS
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (dict.contains(s)) return true;
if (s==null || s.length()==0) return false;
return helper(s, 0, dict);
}
public boolean helper(String s, int i, Set<String> dict) {
int n = s.length();
if (i>=n) return false;
int j = i;
while (j<n) {
while(j<n && !dict.contains(s.substring(i,j+1))) {
j++;
}
if (j==n) return false;
boolean goodAfter = helper(s, j+1, dict);
if (goodAfter) return true;
j++;
}
return false;
}
}
我不确定对: 没有额外空间,空间O(1)。每次recursion,都要loop O(n), 时间O(n^n)
毫无意外是会超时的,要加速基本要上DP了。关键是DP记录什么东西不好想,反正我是想了很久都没想到。最后看了大牛的答案才明白。还是bottom up approach。比如String长度为0,问题成立吗? 然后String长度为1,成立吗? 一直到n。所以dp就是记录从头开始的substring的长度和问题能否成立的关系。关键是dp[i]怎样可以利用dp[k], k=0,.., i-1的结果?就要在找0到i-1中找到一个k,使得dp[k] && dict.contains(s.substring(k, i))为真。意义是从0到k-1之间的substring,已经有办法用字典的词组成了,而且如果k到i-1之间的substring也在字典里,那么从0开始,长度为i的string就可以由字典里的词组成。
注意的是dp[0] == true是因为如果整个词都在字典里,那么就可以由字典的词组成。
优化解法:一维DP
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
int n = s.length();
boolean[] dp = new boolean[n+1];
dp[0] = true;
for (int i=1; i<=n; i++) {
for (int j=0; j<i; j++) {
if (dp[j] && dict.contains(s.substring(j,i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
}
空间 O(n), 时间O(n^2)
这种方法好像在leetcode很常见,以后要总结一下。
http://stupidcodergoodluck.wordpress.com/2013/11/15/leetcode-word-break/
leetcode-word break-ZZ的更多相关文章
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- 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 && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- float 浮动 文档流和文字流区别
关于float属性的脱离文档流的问题 使用float浮动后,元素虽然会脱离文档流,但还处在文本流的位置当中,所以就不会出现重叠的效果吗? 下面我自己试了一下,给两个DIV分别设置了样式,而只给第一个D ...
- NestJS 用TypeScript开发 nodeJS后端
NestJS A progressive Node.js framework for building efficient and scalable server-side applications ...
- 小y的质数
题目链接:https://ac.nowcoder.com/acm/contest/634/C 链接:https://ac.nowcoder.com/acm/contest/634/C来源:牛客网 题目 ...
- DB2 锁问题的监控和解决
常见的锁问题包括: 锁等待 锁超时 锁升级 死锁 而根据问题的特性通常分为两种: 实时事件:问题正在发生 历史事件:问题已经过去 如果实时事件,DBA 可以通过查看表信息.GET SNAPSHOT 或 ...
- 【DB2】普通用户最小查询权限分配
1. 通过实例用户或者有dbadm权限的用户连接数据库 db2 connect to <db-name> 2. 分配普通用户连接权限db2 "grant connect on d ...
- Python数据类型(列表)
文章内容参考了教程:http://www.runoob.com/python/python-basic-syntax.html#commentform Python 列表(List) 序列是Pytho ...
- ios UITableView 异步加载图片并防止错位
UITableView 重用 UITableViewCell 并异步加载图片时会出现图片错乱的情况 对错位原因不明白的同学请参考我的另外一篇随笔:http://www.cnblogs.com/lesl ...
- html控件
checkbox val = "<li class='layer'><label><input type='checkbox' checked name='la ...
- whatwg-fetch
fetch 是什么 XMLHttpRequest的最新替代技术 fetch优点 接口更简单.简洁,更加语义化 基于promise,更加好的流程化控制,可以不断then把参数传递,外加 async/aw ...
- 封装常用的跨浏览器的事件对象EventUtil
var EventUtil = { addHandler: function(element,type,handler) { //事件监听 if(element.addEventListener) { ...