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. JAVA每日一旅3

    1.关于byte byte在内存中占一个字节,范围是-128-127,128作强制类型转换到byte变成-128,因为128的二进制表示:1000 0000,最高位是符号位. 2.关于Hibernat ...

  2. Cocoa Drawing

    Graphics Contexts Graphics contexts are a fundamental part of the drawing infrastructure in Cocoa ap ...

  3. PAT 07-3 求素数

    求素数,这是一个“古老”的问题,每个学过编程的人都应该碰到过,这里是求第M+1到第N个素数,这么经典的问题,当然得给它写上一笔,下面是题设要求及代码实现 /* Name: Copyright: Aut ...

  4. allegro si(三)

    前言:si的教程市面上是很少的,layout是台湾工程师的强项,还有就是日本人,国人爱用AD. si的教程中靠谱的还是张飞的收费课程,还有华为的资料. Cadence SI 仿真实验步骤如下: 1.熟 ...

  5. javascript正则表达式替换字符串

    var reg = /^per_list(.*)[\d]{1,}(.*)/;var str = "per_listAmtApril1.value";var replaceStr = ...

  6. php大力力 [009节]php在百度文库的几个基础教程

    2015-08-23 php大力力009. php在百度文库的几个基础教程 php大力力 [009节]php在百度文库的几个基础教程 PHP脚本中操作MySQL数据库3-猿代码平台 php基础教程-绝 ...

  7. Android屏幕适配常识

    屏幕适配的注意事项 1. AndroidManifest.xml设置 在中Menifest中添加子元素 android:anyDensity="true"时,应用程序安装在不同密度 ...

  8. 7、网页制作Dreamweaver(悬浮动态分层导航)

    悬浮动态分层导航的制作: 1.首先在<head>里面引用一个JQUERY的文件以用来制作鼠标点击动画效果(从网站上下载即可) <script language="javas ...

  9. HDU2222 (AC自动机)

    AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...

  10. Unity3d Web Player 与server端联网配置

    针对Unity3d Web Player 的server端联网配置写一随笔咯.  以SmartFoxServer2X官方的Unity3d Example ”tris“为例,部署好服务器之后,在Unit ...