Word Break II leetcode 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"]
.
题解:
这道题不仅仅是看是不是wordbreak,还需要在此基础上把所有word break的结果保存。
为了把所有可能性都保存,那么就使用DFS方法来解决。DFS主要就是跳的层次不容易看出,我下面就以字符串leetcode字典le l et eet code作为例子画了一张图,大概讲解了如何递回和返回,这样更加有助于理解。
代码如下:
- 1 public boolean wordBreakcheck(String s, Set<String> dict) {
- 2 if(s==null || s.length()==0)
- 3 return true;
- 4 boolean[] res = new boolean[s.length()+1];
- 5 res[0] = true;
- 6 for(int i=0;i<s.length();i++){
- 7 StringBuilder str = new StringBuilder(s.substring(0,i+1));
- 8 for(int j=0;j<=i;j++){
- 9 if(res[j] && dict.contains(str.toString())){
- res[i+1] = true;
- break;
- }
- str.deleteCharAt(0);
- }
- }
- return res[s.length()];
- }
- public ArrayList<String> wordBreak(String s, Set<String> dict) {
- ArrayList<String> res = new ArrayList<String>();
- if(s==null || s.length()==0)
- return res;
- if(wordBreakcheck(s,dict))
- helper(s,dict,0,"",res);
- return res;
- }
- private void helper(String s, Set<String> dict, int start, String item, ArrayList<String> res){
- if(start>=s.length()){
- res.add(item);
- return;
- }
- StringBuilder str = new StringBuilder();
- for(int i=start;i<s.length();i++){
- str.append(s.charAt(i));
- if(dict.contains(str.toString())){
- String newItem = new String();
- if(item.length()>0)
- newItem = item + " " + str.toString();
- else
- newItem = str.toString();
- helper(s,dict,i+1,newItem,res);
- }
- }
- }
Reference: http://blog.csdn.net/linhuanmars/article/details/22452163
Word Break II leetcode java的更多相关文章
- Word Break II——LeetCode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Word Ladder II leetcode java
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Word Break II -- leetcode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 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 ...
- 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 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
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- CSUOJ 1009 抛硬币
Description James得到了一堆有趣的硬币,于是决定用这些硬币跟朋友们玩个小游戏.在一个N行M列的表格上,每一个第i行第j列的格子上都放有一枚James的硬币,抛该硬币正面朝上的概率为Pi ...
- [leetcode tree]103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型
Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型 认识Highmaps Highmaps是Highcharts的姊妹框架,用来实现地图图表.它完全使用Javascript ...
- Python实现QQ自动点赞
用Python做一个QQ自动点赞神器,上代码: 1 def QQZan(qq): 2 browser = webdriver.Chrome() 3 browser.maximize_window() ...
- thinkphp5使用redis
1.设置应用配置文件config.php type可以是很多分类File.Redis等等 2.thinkphp5使用redis新建application/index/controller/index. ...
- 【BZOJ 3661】 Hungry Rabbit (贪心、优先队列)
3661: Hungry Rabbit Time Limit: 100 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 67 Solved: 4 ...
- 递归与分治策略之棋盘覆盖Java实现
递归与分治策略之棋盘覆盖 一.问题描述 二.过程详解 1.棋盘如下图,其中有一特殊方格:16*16 . 2.第一个分割结果:8*8 3.第二次分割结果:4*4 4.第三次分割结果:2*2 5.第四次分 ...
- python 中__name__ = '__main__' 的作用,到底干嘛的?
python 中__name__ = 'main' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and execu ...
- Java 接口与抽象类
抽象类 <JAVA编程思想>一书中,将抽象类定义为"包含抽象方法的类".只要用abstract修饰的类就是抽象类,抽象类不一定包含抽象方法,但有抽象方法的类一定是抽象类 ...
- 利用Everything开启http服务测试移动端浏览器环境
一.Everything 简介 Everything本身是一款小巧的文件搜索神器,可以快速的搜索电脑中的文件,速度非常快. 二.使用Everything的http服务 在做移动端浏览器页面时,有时需要 ...