LeetCode_DP_Word Break II
LeetCode_Word Break II
一、题目描写叙述:
二、解决思路:
题目要求我们要在原字符串中加空格,使得隔开的每一个词都是词典中的词。
所以我们大能够按顺序扫描每一个字符。可是然后当碰到是词典中的词。就加个空格,可是要求返回的结果按题目的提醒是个List,说明有非常多分隔方式。
再细细想问题。我们发现第二个词能被成功分隔出来,是由于第一个词已经分出来了。依次类推;所以我们能够採用动态规划,设置初值dp[0] 是一个 List;递推式是 dp[n] = dp[n-1] && a[n] (a[n] = dict.contains(每次截断的字符串))。
这样,我们通过动态规划。就获得了字符串中每一个字符索引处的单词。
dp[10] = {“dog”}, dp[7]={“and”,”sand”}, dp[4]={“cats”}, dp[3]={“cat”}, dp[0]={},其它都是null。
最后我们要做的就是按顺序组合字符串,放到List中,返回。怎么组合呢?这就能够利用dfs递归。在我理解,能够把递归想象成一个栈。重要的是设置结束条件和在剔除栈顶元素。
三、Java代码:
public class Solution {
public List<String> wordBreak(String s, Set<String> wordDict) {
List<String>[] dp = new ArrayList[s.length()+1];
dp[0] = new ArrayList<String>();//设置初值
for(int end=1; end<=s.length(); end++) {//substring的endIndex
for(int start=end-1; start>=0; start--) {
String word = s.substring(start,end);
if(dp[start] != null && wordDict.contains(word)) { //动态规划递推式
if(dp[end] == null) {
dp[end] = new ArrayList<String>();
}
dp[end].add(word);
}
}
}
List<String> result = new ArrayList<String>();
if(dp[s.length()] == null)
return result;
else {
List<String> temp = new ArrayList<String>();
dsfStringList(dp,s.length(),result,temp);
return result;
}
}
private static void dsfStringList(List<String>[] dp, int end, List<String> res, List<String> temp) {
if(end <= 0) { //递归结束条件
String s = temp.get(temp.size()-1);
for(int i = temp.size()-2; i >= 0; i--)
s += (" "+temp.get(i));
res.add(s);
return;
}
for(String str : dp[end]) {
temp.add(str);
dsfStringList(dp, end-str.length(), res, temp); //递归式
temp.remove(temp.size()-1);
}
}
}
希望与大家多多交流。
LeetCode_DP_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 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- 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】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: 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 ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 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 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- Java实现——字符串分割以及复制目录下的所有文件
0. 前言 今天有个需求,把Android中data/data目录下指定(通过读预置的XML文件)的多个应用下的多个目录全部内容通过OTG模式复制到U盘中. 首先读取XML文件内的某个节点的属性值, ...
- Python基础之字符串,布尔值,整数,列表,元组,字典,集合
一.str字符串 1.capitalize字符串首字母大写 name = "json" v = name.capitalize() print(v) # 输出结果:Json 2.c ...
- cf886d Restoration of string
明确几点 假设有串 ab,那么 a 后头必须是 b,b 前头必须是 a,否则就不是最频繁的了. 不可成环,aba是非法的. #include <iostream> #include < ...
- Webstrom卡顿问题解决
1.设置node_modules 打开项目,新建node_modules空文件夹,然后右击选择Mark Directory as,选择Excluded. 2.设置ingore文件 files-> ...
- hdu_2092_整数解
枚举 #include <iostream> #include <cstdio> #include <cmath> using namespace std; int ...
- ubuntu添加开机启动
vim /etc/init.d/mytest #!/bin/sh echo "$(pwd) and $USER and $(whoami)" >> /root/temp ...
- 【Go】并发编程
Go语言宣扬用通讯的方式共享数据. Go语言以独特的并发编程模型傲视群雄,与并发编程关系最紧密的代码包就是sync包,意思是同步.同步的用途有两个,一个是避免多个线程在同一时刻操作同一个数据块,另一个 ...
- [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)
传送门 告诉你一些点不能到达1,由于是双向边,也就是1不能到达那些点 那么从1开始dfs,如果当前点能到达不能到达的点,那么当前点就是损坏的. #include <cstdio> #inc ...
- noip2018——题解&总结
近期正在疯狂复习某些东西,这篇博客尽量年底更完……(Day2T2除外) 好了,所有的希望都破灭了,原来这就是出题人的素质.——一个被欺骗的可怜 $OIer$ 人生中倒数第三次 $noip$ (Mayb ...
- testng自定义html报告,根据freemaker生成
[转] https://testerhome.com/topics/3487 [参考]https://www.cnblogs.com/cheese320/p/8890929.html 做了些修改,换 ...