题目, 反正就是一个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的更多相关文章

  1. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  2. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  3. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  5. 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 ...

  6. 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 ...

  7. [LeetCode] Word Break II 解题思路

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. [Leetcode] 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: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

  10. [LeetCode] Word Break 拆分词句

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. js复习知识点

    null和undefined具体在什么时候使用? 如果定义的变量准备用来保存对象,那么最好用将改变量初始化为null而不是其他值 如果未定义的值可以用空字符 undefined是null派生出来的,所 ...

  2. oracle--dump & V$BH

    一,什么是BH BH即Buffer Header,每一个数据块在被读入buffer cache时,都会先在buffer cache中构造一个buffer header,buffer header与数据 ...

  3. golang io.ReadFull

    buf := make([]byte, 10, 10) file, _ := os.Open("./data.txt") n, err := io.ReadFull(file, b ...

  4. Python取时间,日期的总结

    import datetime from datetime import timedelta now = datetime.datetime.now() #今天 today = now #昨天 yes ...

  5. hibernate 自动创建表中文乱码问题

    <property name="connection.url" > <![CDATA[jdbc:mysql:///test?useUnicode=true& ...

  6. 聚焦小游戏技术生态,腾讯游戏云GAME-TECH落地厦门

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云 发表于云+社区专栏 2018迎来了小游戏元年,据<2018年小游戏行业白皮书>显示:2018年小游戏市场规模预 ...

  7. pyhon-爬虫实战抓取豆瓣top250到mysql

    采集地址https://movie.douban.com/top250 一.创建mysql数据库 CREATE TABLE `t_doubantop` ( `id` int(11) unsigned ...

  8. .netCore2.0 依赖注入

    依赖注入(ID)是一种实现对象及其合作者或者依赖想之间松散耦合的技术对于传统的方法来说,获取类的方法通常用new如下 public class DIController : Controller { ...

  9. 特殊引用类型(string)

    private string FuncWithParameters(int param1, String param2, List<string> param3) { // 我们在这里改变 ...

  10. NOPI 导出 Excel 2007

    代码: public static void ThisTo<T>( List<T> source, string[] colums, Func<T, object[]&g ...