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 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"].
解题思路:
参考上题思路二的解法,修改下代码,让其显示路径即可,JAVA实现如下:
static public List<String> wordBreak(String s, Set<String> wordDict) {
List<String> list = new ArrayList<String>();
dfs(list, s, wordDict, new HashSet<String>(), new ArrayList<String>());
return list;
}
static public boolean dfs(List<String> list, String s, Set<String> dict,
Set<String> unmatch, List<String> alist) {
boolean isMatch=false;
for (String prefix : dict) {
if (s.equals(prefix)) {
alist.add(prefix);
StringBuilder sb = new StringBuilder();
for (String str : alist) {
sb.append(str);
sb.append(" ");
}
list.add(sb.substring(0, sb.length() - 1));
alist.remove(alist.size() - 1);
isMatch=true;
continue;
} else if (s.startsWith(prefix)) {
String suffix = s.substring(prefix.length());
if (!unmatch.contains(suffix)) {
alist.add(prefix);
if (!dfs(list, suffix, dict, unmatch, alist))
unmatch.add(suffix);
else isMatch=true;
alist.remove(alist.size() - 1);
}
}
}
return isMatch;
}
Java for LeetCode 140 Word Break II的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【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 ...
随机推荐
- 第七节 JBPM 中的脚本语言
1.JPDL表达式 2.动作:数据库操作例子 3.路由:transaction一个流程之间的指向 4.BeanShell脚本语言 例子: 发布到数据库中才能做一个测试类
- BZOJ-1228 E&D 博弈SG+找啊找啊找规律
讨厌博弈,找规律找半天还是错的.... 1228: [SDOI2009]E&D Time Limit: 10 Sec Memory Limit: 162 MB Submit: 666 Solv ...
- NOIP2013 货车运输 (最大生成树+树上倍增LCA)
死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...
- 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
在部署到IIS7时,MVC3报了一个找不到资源的错误,文件肯定是有的,而且页面是肯定报错的,也就说内部运行错误了,而MVC把错误没有抛出来而已: 所以对症下药,发觉我的项目里面用了rexs进行多语言, ...
- POJ 2796 Feel Good
传送门 Time Limit: 3000MS Memory Limit: 65536K Case Time Limit: 1000MS Special Judge Description Bill ...
- unix/linux进程详解
技术分享 启动新进程 stdlib.hintsystem(const char *string)whichequals to "sh -c string" 替换进程映像unistd ...
- $root knockout
http://www.cnblogs.com/rohelm/p/3209757.html 以列表方式呈现数据 处理以数组形式储存的多条数据,要先认识foreach.在ViewModel定义一个Jav ...
- Ajax技术详解
Ajax技术:Ajax描述了一种主要使用脚本(JS)操纵HTTP的web应用架构,它的主要特点是使用脚本操纵HTTP和web服务器进行数据交换,不会导致页面重载.Ajax的核心是JS的XMLHttpR ...
- javaScript模块化规范ADM与CMD
模块化:模块化是指在解决某一个复杂问题时,依照一种分类的思维把问题进行系统性的分解处理,可以想象一个巨大的系统代码,被整合优化分割成逻辑性很强的模块时,对于软件是一种何等意义的存在. 模块化系统所必须 ...
- Lua中的捕获
Lua中的捕获 捕获 捕获是这样一种机制:可以使用模式串的一部分匹配目标串的一部分.将你想捕获的模式用圆括号括起来,就指定了一个捕获.在string.find使用捕获的时候,函数会返回捕获的值作为 ...