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 ...
随机推荐
- Angular 2 升级到 Angular 5
Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...
- php查询某个字段指定值的所有条数
一.查询某个字段指定值的所有条数 以name叫张三的人为例,查询表中叫张三的人的总数 $where['name']='张三'; $count=M('table')->where($where)- ...
- 递归demo
递归算法就是直接或间接调用自己的算法 public static void main(String[] args) { int m = sum(9); System.out.println(m); } ...
- dgango 报错: Timeout when reading response headers from daemon process
问题: image = np.asarray(bytearray(f.read()), dtype="uint8")cv2_img = cv2.imdecode(image, cv ...
- javascript记住用户名和登录密码
javascript记住用户名和登录密码 下面主要通过代码给大家展示下javascript记住用户名和登录密码,具体代码内容请看下文. <script type="text/javas ...
- 大数据技术之_16_Scala学习_13_Scala语言的数据结构和算法_Scala学习之旅收官之作
第十九章 Scala语言的数据结构和算法19.1 数据结构(算法)的介绍19.2 看几个实际编程中遇到的问题19.2.1 一个五子棋程序19.2.2 约瑟夫问题(丢手帕问题)19.2.3 其它常见算法 ...
- 【LESS系列】基本语法
这里将直接以实例的方式展示 LESS 的基本语法. less code 是编译前的代码,css code 是编译后的代码. 本文的内容,同样是引自[http://www.ibm.com/develop ...
- 第一次尝试用ANT进行build
虽然是软件工程专业学生,但很多东西都才刚刚接触,有些惭愧,但我相信“Later better than never”,所以我还是鼓励自己不断学习,以后尽量把自己新学会的东西记录下来,以此来督促自己的学 ...
- 【关于迭代器的for-each遍历集合现象。。。。。】
foreahc迭代集合元素的同时修改集合元素抛异常..ConcurrentModificationException异常 只要使用迭代器遍历,其他集合遍历时进行增删操作都需要留意是否会触发Concur ...
- PD虚拟机修改RemixOS的屏幕分辨率
PD虚拟机修改RemixOS的屏幕分辨率 2017年12月02日02:13:55 by SemiconductorKING 最近要用个移动端APP,手机不方便就想在电脑跑一个,然后装了个以前用过的觉得 ...