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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 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 ...

  4. 【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 ...

  5. 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 ...

  6. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  7. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  8. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  9. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

随机推荐

  1. 关于加号传递到后端会变为空格的c#例子

    参考博客:http://blog.csdn.net/nsdnresponsibility/article/details/50965262 以前在一次传递参数的情况中遇到,特此记录一下. 之前传递的参 ...

  2. [android开发篇]项目目录结构

  3. 关于JS正则表达式

    去除所有P标签 content=content.replace(/<([\/]?)(p)((:?\s*)(:?[^>]*)(:?\s*))>/g, ''); 将所有的  1.     ...

  4. BZOJ 2176 Strange string ——最小表示法

    本来想用来练习后缀自动机的,但是100w有点虚(事实证明确实T掉了). 只好上最小表示法. #include <cstdio> #include <cstring> #incl ...

  5. BZOJ 3527 [Zjoi2014]力 ——FFT

    [题目分析] FFT,构造数列进行卷积,挺裸的一道题目诶. 还是写起来并不顺手,再练. [代码] #include <cmath> #include <cstdio> #inc ...

  6. BZOJ2288 【POJ Challenge】生日礼物 【堆 + 链表】

    题目 ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物. 自然地,ftiasch想要知 ...

  7. ElasticSearch 索引查询使用指南——详细版

    我们通常用用_cat API检测集群是否健康. 确保9200端口号可用: curl 'localhost:9200/_cat/health?v' 绿色表示一切正常, 黄色表示所有的数据可用但是部分副本 ...

  8. POJ2486 Apple Tree

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Wshxzt is ...

  9. msp430入门学习41

    msp430的其他九 msp430入门学习

  10. web信息搜索之目标扫描篇

    https://blog.csdn.net/dongfei2033/article/details/78175421