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 ...
随机推荐
- 【C语言学习】-01 C基础
本文目录: 0.进制转换 1.C数据类型 2.常量变量 3.运算符 4.表达式 5.格式化输入输出 回到顶部 0.进制转换 在计算机中存储的数据,主要是以二进制形式存在,而我们生活中主要使用的有十进制 ...
- Java选择结构、循环结构
1:switch语句(掌握) (1)格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+1; ...
- 捕获异常的两种方式Exception
1.抛出异常:让调用此方法的代码去管 public static void GetFile() throws Exception{} package com.throwable; import jav ...
- 针对电信乌龙事件的深度测试: 广州电信错误将深圳地区189的号码在3G升级4G申请时从广州网厅发货,造成深圳用户收到4G卡后无法激活,深圳电信找不到订单
广州电信错误将深圳地区189的3G升级4G申请从中国电信广州网厅发货(智能卡号:8986 1114 9002 0851 742X S 电话号码 189),造成用户收到4G卡后无法激活,深圳电信找不 ...
- 使用Dmitry Sklyarov的方法来破解一款流行的4G调制解调器
逆向工程师如果对设备代码和固件系统进行检测时,发现了许多经过加密处理的固件文件,该怎么办?在本文中我将通过一个真实的故事来教大家如何利用一些基础的计算机知识以及简单的逻辑来应对这一问题. 因为这篇文章 ...
- electronic data interchange 电子数据交换
electronic data interchange 电子数据交换
- Qt发送HTTP请求
http://hi.baidu.com/cmdmac/item/c45b9f0fb0d0938802ce1bbd 最近在搞QT跟服务器交互的东西,自然少不了发送和接受HTTP请求.在网上找了一些资料知 ...
- JQuery源码分析(三)
jQuery中ready与load事件 jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //document read ...
- IPhone手机自动添加到itunes设置
一,项目设置 如图:点击项目--info 在key下面条目上右键点击,选择添加Application supports iTunes file sharing value设置为yes
- [转]Table-Driven and Data Driven Programming
What is Table-Driven and Data-Driven Programming? Data/Table-Driven programming is the technique of ...