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. 算法学习记录-图——最小路径之Floyd算法

    floyd算法: 解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径问题,同时也被用于计算有向图的传递闭包. 设为从到的只以集合中的节点为中间节点的最短路径的长度. 若最短路径经过 ...

  2. JavaScript onload

     The onload event occurs immediately after a page or an image is loaded.onload事件当一个页面或是一张图片加载完成时被触发. ...

  3. EasyUI 打印当前页

    function CommonPrint(printDatagrid, type) { var tableString = '<table cellspacing="0" c ...

  4. [AHOI2009]维护序列 (线段树)

    题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...

  5. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

  6. 【2018.10.18】noip模拟赛Day2 地球危机(2018年第九届蓝桥杯C/C++A组省赛 三体攻击)

    题目描述 三体人将对地球发起攻击.为了抵御攻击,地球人派出了 $A × B × C$ 艘战舰,在太 空中排成一个 $A$ 层 $B$ 行 $C$ 列的立方体.其中,第 $i$ 层第 $j$ 行第 $k ...

  7. bzoj2324 [ZJOI2011]营救皮卡丘 费用流

    [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2653  Solved: 1101[Submit][Status][D ...

  8. Spoj-BITDIFF Bit Difference

    Given an integer array of N integers, find the sum of bit differences in all the pairs that can be f ...

  9. vue elementUI 表单校验(数组多层嵌套)

    在使用vue element-ui form表单渲染的时候,会遇到这样的数据结构: { "title":''123455, "email":'123456@qq ...

  10. PatentTips - Optimizing Write Combining Performance

    BACKGROUND OF THE INVENTION The use of a cache memory with a processor facilitates the reduction of ...