leetcode — word-break-ii
import java.util.*;
/**
* Source : https://oj.leetcode.com/problems/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"].
*/
public class WordBreak2 {
/**
* 找到所有可能的分割方法,使得分割后的单词在dic中
*
* 使用DFS
*
* @param str
* @param dic
* @return
*/
public String[] wordBreak (String str, Set<String> dic) {
List<String> result = new ArrayList<String>();
List<String> solution = new ArrayList<String>();
recursion(str, dic, solution, result);
return result.toArray(new String[solution.size()]);
}
private void recursion (String str, Set<String> dic, List<String> solution, List<String> result) {
if (str.length() == 0 && solution.size() > 0) {
StringBuffer stringBuffer = new StringBuffer();
for (String word : solution) {
stringBuffer.append(word);
stringBuffer.append(" ");
}
result.add(stringBuffer.substring(0, stringBuffer.length()-1));
}
for (int i = 0; i < str.length(); i++) {
if (dic.contains(str.substring(0, i+1))) {
solution.add(str.substring(0, i+1));
recursion(str.substring(i+1, str.length()), dic, solution, result);
solution.remove(solution.size()-1);
}
}
}
public static void main(String[] args) {
WordBreak2 wordBreak2 = new WordBreak2();
String[] dicStr = new String[]{"cat", "cats", "and", "sand", "dog"};
System.out.println(Arrays.toString(wordBreak2.wordBreak("catsanddog", new HashSet<String>(Arrays.asList(dicStr)))));
}
}
leetcode — word-break-ii的更多相关文章
- 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:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [LeetCode] Word Break II (TLE)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
随机推荐
- 小随笔:利用Shader实现模型爆炸和沙粒化的效果
0x00 前言 上一篇小随笔<小随笔:利用Shader给斯坦福兔子长毛和实现雪地效果>中,我和大家聊了聊著名的斯坦福兔子和利用geometry shader实现的一些效果.这篇文章继续沿用 ...
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
JAVA基础再回首(二十五)--Lock锁的使用.死锁问题.多线程生产者和消费者.线程池.匿名内部类使用多线程.定时器.面试题 版权声明:转载必须注明本文转自程序猿杜鹏程的博客:http://blog ...
- Android Studio怎样选择查看指定进程的log?
今天说一个简单的东东,关于Android Studio在logcat中怎样查看指定进程的log. 关于过滤你自己所开发的应用的log.在这篇文章中:Android Studio怎样给log加入过滤项( ...
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
android提供了统一的编程模型Intent来启动系统的组件,这样提供了松耦合性.是一种mvc的编程模式 $(function () { $('pre.prettyprint code').each ...
- COCOS学习笔记--关于使用cocostudio打安卓包
我在之前的博客里也写到过,如今cocos引擎提供了一键打包.很方便. 今天正好做个測试,想用引擎的一键打包弄个apk的安卓包.以下就简介一下详细过程和自己的一些理解: 先说一下项目背景,因为我的电脑是 ...
- Python笔记·第四章—— 细数Python中的数据类型以及他们的方法
一.数据类型的种类及主要功能 1.数字类型 数字类型主要是用来计算,它分为整数类型int和浮点类型float 2.布尔类型 布尔类型主要是用于判断,它分为真True和False两种 3.字符串类型 字 ...
- Windows环境下部署Tomcat服务器图文教程
Tomcat是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. 本文将详细介绍在Windows环境下 ...
- 大数据学习(2)HDFS文件管理
命令行管理HDFS [root@server1 bin]# hadoop fs Usage: hadoop fs [generic options] [-appendToFile <locals ...
- ServerSuperIO Designer IDE 发布,打造物联网通讯大脑,随心而联。附:C#驱动源代码。
1.概况 注:ServerSuperIO Designer IDE 同行业网友随便使用,不涉及到软件使用限制的问题. 从2015年到现在的将近两年的时间,一直在开发.完善ServerSuperIO(S ...
- org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
异常信息 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable represen ...