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 ... 
随机推荐
- Servlet,GenericServlet和HttpServlet的继承关系
			HttpServlet是GenericServlet的子类. GenericServlet是个抽象类,必须给出子类才能实例化.它给出了设计servlet的一些骨架,定义了servlet生命周期,还有一 ... 
- 【CodeForces 625A】Guest From the Past
			题 题意 一升奶可以花费a元,也可以话b元买然后获得c元,一开始有n元,求最多买多少升奶. 分析 贪心,如果b-c<a,且n≥b,那就买b元的,n先减去b再看看够买多少瓶,然后再+1瓶,余款再购 ... 
- 【codevs1409】 拦截导弹 2
			http://codevs.cn/problem/1409/ (题目链接) 题意 给出n个三维的导弹,每次拦截只能打x,y,z严格上升的若干个导弹,求最多能一次拦截下多少个导弹,以及最少拦截几次将所有 ... 
- PHP Code Reviewing Learning
			相关学习资料 http://code-tech.diandian.com/post/2012-11-04/40042129192 http://ssv.sebug.net/高级PHP应用程序漏洞审核技 ... 
- Android 设计模式 之 单例模式
			http://blog.csdn.net/fangchongbory/article/details/7734199 目录(?)[+] 单例模式常见情景 首先实现1中的单例模式A 实现2中单例模式 ... 
- 求三数中Max和猜拳游戏
			方法一: Console.WriteLine("请输入三个数字:"); int a = int.Parse(Console.ReadLine()); int b = int.Par ... 
- stl-基本知识
			摘要:本文列出几个基本的STL map和STL set的问题,通过解答这些问题讲解了STL关联容器内部的数据结构,最后提出了关于UNIX/LINUX自带平衡二叉树库函数和map, set选择问题,并分 ... 
- SpringServletContext简单案例
			一.项目结构及相应jar包,如下图 二.UserService代码 package com.hjp.service; /** * Created by JiaPeng on 2015/11/15. * ... 
- 锋利的jQuery-5--下拉框的应用(看写法)
			如图,可以通过中间的按钮将左边选中的选项添加到右边,或者全部添加到右边,也可通过双击添加.反之也可以. 左边选中加到右边: $("#add").click(function(){ ... 
- Starting zabbix_agentd:  No such file or directory
			问题描述 [root@localhost admin]# service zabbix_agentd restart Shutting down zabbix_agentd: [FAILED] Sta ... 
