题目, 反正就是一个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. 整理的最全 python常见面试题

      整理的最全 python常见面试题(基本必考)① ②③④⑤⑥⑦⑧⑨⑩ 1.大数据的文件读取: ① 利用生成器generator: ②迭代器进行迭代遍历:for line in file; 2.迭代 ...

  2. spring data jpa 动态查询(工具类封装)

    利用JPA的Specification<T>接口和元模型就实现动态查询了.但是这样每一个需要动态查询的地方都需要写一个这样类似的findByConditions方法,小型项目还好,大型项目 ...

  3. Oracle 多行、多列子查询

    本文使用到的是oracle数据库scott方案所带的表,scott是oracle数据库自带的方案,使用前请确保其解锁 一.多行子查询 多行子查询子查询是嵌入在其他Sql语句中的select语句,Ora ...

  4. SqlHelper中SqlHelperParameterCache类的用法介绍

    SqlHelper类中提供了三种可以用来管理SqlParameter参数的共享方法.下面来一一讲解: 1.CacheParameterSet 将SqlParameter参数数组存储到本地缓存中 2.G ...

  5. MySQL子查询subquery

    子查询(Subquery)是指出现在其他SQL语句内的SELECT子句. 例如: select * from t1 where col1=(select col2 from t2); 其中select ...

  6. CSS box-flex属性,然后弹性盒子模型简介

    今天做项目的时候发现一个css3的新属性flex 一.什么是flex 它的作用是能够按照设置好的规则来排列容器内的项目,而不必去计算每一个项目的宽度和边距.甚至是在容器的大小发生改变的时候,都可以重新 ...

  7. 用泛型T替代object做为万能参数传递

    using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public cla ...

  8. Linux上用户之间对话

    Linux上用户之间对话 昨天想在CentOS7上与另外一个用户对话,但把命令忘记了,特此记录下来. Write命令 write命令是单向发送一条消息给同机器的Linux用户.首先通过who命令查看谁 ...

  9. ActiveMQ_Linux安装

    首先ActiveMQ查看你需要的版本 官网:http://activemq.apache.org/ 我这里选择的是:apache-activemq-5.14.0-bin.tar.gz 然后在linux ...

  10. codeforces 675 C ——Money Transfers——————【思维题】

    Money Transfers time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...