leetcode 140. Word Break II ----- java
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的更多相关文章
- [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 ...
- 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 ...
- Leetcode#140 Word Break II
原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- 【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 ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【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】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
随机推荐
- WP8.1 Study4:WP8.1中控件集合应用
1.AutoSuggestBox的应用 在xaml里代码可如下: <AutoSuggestBox Name="autobox" Header="suggestion ...
- K2认证考试,为竞争力加分
思科Cisco认证.ADOBE认证.微软认证.印度AIIT认证.华为认证.IBM认证等……在各种“证”满天飞的时候,如何独具慧眼选择最有含金量的证书? 八成 的人力资源主管会检查求职者的认证情况.(C ...
- vue js 用nodejs的依赖包 --2016-08-23
今天被nodejs包依赖坑了一下,上次上传的项目突然运行不起来了,原来是package.json中定义了使用最新版本的依赖,而最新版本有可能调整了结构或者改了api,比如vux把flexbox-it ...
- BinaryWriter
c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...
- JS获取客户端Mac和IP
JS获取硬件信息是通过ActiveX进行获取的,因此只能IE浏览器支持,火狐不支持 而且必须降低浏览器安全级别,因此不到万不得以一般不会采用这种方式 <html> <head> ...
- hdu1116 欧拉回路
//Accepted 248 KB 125 ms //欧拉回路 //以26个字母为定点,一个单词为从首字母到末尾字母的一条边 //下面就是有向图判断欧拉回路 //连通+节点入度和==出度和 或者 存在 ...
- IOS9适配 MARK
最近做了iOS 9的适配,程序出现大量警告也做了些处理,写出来分先给大家. 一.iOS 9适配 问题一: <Error>: CGContextSaveGState: invalid con ...
- FR #2题解
A. 考虑把(u,v)的询问离线挂在u上,然后dfs,每次从fath[x]到[x]相当于x子树dis区间加1,x子树以外区间-1,然后维护区间和区间平方和等. 常数略大. #include<io ...
- CRM客户关系管理系统 ——客户联系人添加(十五)
需求描述: 1.业务员自己可以查看属于自己的客户信息 2.客服部经理可以查看查看所有客户信息 3.其他人员不得查看客户信息 效果截图:
- iOS 静态库和动态库的区别&静态库的生成
linux中静态库和动态库的区别 一.不同 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态库和动态库两种. 1. 静态函数库 这类库的名字一般是libxxx.a:利用静态函 ...