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"].

139题的延伸题,需要的出所有结果。

1、递归,仍旧超时。

public class Solution {
String[] result;
List list; public List<String> wordBreak(String s, Set<String> wordDict) { int len = s.length(),maxLen = 0;
result = new String[len];
list = new ArrayList<String>(); for( String str : wordDict ){
maxLen = Math.max(maxLen,str.length());
}
helper(s,0,wordDict,maxLen,0);
return list; } public void helper(String s,int pos,Set<String> wordDict,int maxLen,int count){ if( pos == s.length() ){
String str = result[0];
for( int i = 1 ; i<count-1;i++){
str =str+ " "+result[i];
}
if( count > 1)
str = str+" "+result[count-1];
list.add(str);
}
int flag = 0;
for( int i = pos;pos - i<maxLen && i<s.length();i++){
if( wordDict.contains( s.substring(pos,i+1) )){
result[count] = s.substring(pos,i+1);
helper(s,i+1,wordDict,maxLen,count+1);
flag = 1;
}
} }
}

2、加入提前判断是否存在答案(即上一题的结论)就可以了。

public class Solution {
String[] result;
List list; public List<String> wordBreak(String s, Set<String> wordDict) { int len = s.length(),maxLen = 0;
result = new String[len];
list = new ArrayList<String>(); for( String str : wordDict ){
maxLen = Math.max(maxLen,str.length());
}
boolean[] dp = new boolean[len];
for( int i = 0 ;i<len;i++){ for( int j = i;j>=0 && i-j<maxLen;j-- ){
if( ( j == 0 || dp[j-1] == true ) && wordDict.contains(s.substring(j,i+1)) ){
dp[i] = true;
break;
}
}
}
if( dp[len-1] == false)
return list; helper(s,0,wordDict,maxLen,0);
return list; } public void helper(String s,int pos,Set<String> wordDict,int maxLen,int count){ if( pos == s.length() ){
String str = result[0];
for( int i = 1 ; i<count-1;i++){
str =str+ " "+result[i];
}
if( count > 1)
str = str+" "+result[count-1];
list.add(str);
}
int flag = 0;
for( int i = pos;pos - i<maxLen && i<s.length();i++){
if( wordDict.contains( s.substring(pos,i+1) )){
result[count] = s.substring(pos,i+1);
helper(s,i+1,wordDict,maxLen,count+1);
flag = 1;
}
} }
}

leetcode 140. Word Break II ----- java的更多相关文章

  1. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  2. Java for LeetCode 140 Word Break II

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

  3. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

  4. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  5. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  6. 【LeetCode】140. Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  7. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  8. 【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 ...

  9. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

随机推荐

  1. Linux-如何查看登陆shell的类型

    输入一个系统不认识的命令(如#ig)获得系统提示 aix/#ig ksh ig not found #echo $ (适用sh/ksh) aix/#echo $ ksh #echo $SHELL(用户 ...

  2. 关于HashMap中的负载因子

    这两天在看HashMap的时候,被负载因子float loadFactor搞得很晕,经过一天的研究,最后理出了自己的一点个人见解. 在HashMap的底层存在着一个名字为table的Entry数组,在 ...

  3. io函数

    io函数一般分为两大类: 系统(不带缓存)调用: 如read.write.open 标准(带缓存)调用: fread.fwrite.fopen 上面说的带缓存/不带缓存是针对用户态的,内核态本身都是带 ...

  4. Program B 暴力求解

    Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maxim ...

  5. [C/C++]C++标准

    本文若如特别说明都引于ISO/IEC 14882:2011 7.声明(Declarations) 声明序列(declaration-seq):    声明(declaration)    声明序列(d ...

  6. data属性

    本框架内置组件以及部分插件可以通过data属性来初始化并使用,通常通过data-toggle来调用API(toggle是触发器的意思,例如我们创建一个navtab标签可以通过为a的data-toggl ...

  7. SharePoint 2013 Nintex Workflow 工作流帮助(六)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 7. Call web service(Integration分组) 一个调用WebService的操作. 自然,配置项 ...

  8. 解决mac eclipse 异常退出后无法打开处于loading状态

    <workspace>\.metadata\.plugins\org.eclipse.core.resources目录,删除文件 .snap

  9. UITouch的用法

    UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得. //这四个方法是UIResponder中得方法 // Generally, all responders wh ...

  10. VS2010编译Qt5.4.0静态库

    http://www.kavenblog.com/?p=375 1.Qt的跨平台十分优秀,但是在Windows上是还是会有许多问题,其中之一就是动态链接库的问题,Qt程序的发布必须带一个体积不小的DL ...