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表示的是以 ...
随机推荐
- 【javascript面试题】5个经典的面试题
问题1: 作用域 看一下下面的代码: (function(){ var a = b =5; })(); console.log(b); 结果会输出什么? 答案: 5 这个问题考查的要点是两个不同的作用 ...
- joyoi2020/lfyzoj114 Rainbow 的信号
位与位间互不影响.一位一位计算. 长度为 \(1\) 的区间,选出概率为 \(1/n^2\).其余区间,选出概率为 \(2/n^2\).(这里的区间 \(l \leq r\)) 枚举右端点.记 \(l ...
- [LoadRunner]LR性能测试结果样例分析
R性能测试结果样例分析 测试结果分析 LoadRunner性能测试结果分析是个复杂的过程,通常可以从结果摘要.并发数.平均事务响应时间.每秒点击数.业务成功率.系统资源.网页细分图.Web服务器资源. ...
- jmeter历史版本下载
https://archive.apache.org/dist/jmeter/binaries/
- Leetcode 391.完美矩形
完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1 ...
- Laya List翻页滚动方案 & List滚动源码解析
Laya List翻页滚动方案 & List滚动源码解析 @author ixenos 2019-03-29 1.List翻页滚动方案 /** * 计算下一页的起始索引, 不足时补足 * @p ...
- 如何构建一个flink sql平台
在本系列前面的文章中,简单介绍了一下Ignite的机器学习网格,下面会趁热打铁,结合一些示例,深入介绍Ignite支持的一些机器学习算法. 如果要找合适的数据集,会发现可用的有很多,但是对于线性回归来 ...
- BZOJ 1974 [Sdoi2010]auction 代码拍卖会 ——动态规划
把每一位上不递减的数拆成1+11+11111+11111+..... 然后就可以把巨大的N从复杂度中消掉,因为随着长度的增加1...111%p出现了循环节. 然后就是从n个数中选出几个使他们结果为0( ...
- websql使用实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 森林 BZOJ 3123
题解: 第k大直接用主席树解决 合并利用启发式合并,将较小的连接到较大的树上 #include<cmath> #include<cstdio> #include<cstd ...