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表示的是以 ...
随机推荐
- PHP-redis命令之 散列(hashes)
二.散列(hashes) 1.hset:设置散列的值 $redis->hset('myhas','field1','hello'); $redis->hset('myhas','field ...
- SQLAlchemy常用操作
Models 只是配置和使用比较简单,因为他是Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy 真正算得上全面的ORM框架必然是我们的SQLAlch ...
- poj2217 Secretary 后缀数组
#include <iostream> #include <cstring> #include <string> #include <cstdio> u ...
- HDU 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- Educational Codeforces Round 24
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- java面向抽象编程样例
import java.util.*; abstract class Geometry{ public abstract double getArea(); } class Pillar ...
- python ConfigParser 学习
[安装] ConfigParser 是解析配置文件的第三方库,需要安装 pip install ConfigParser [介绍] ConfigParser 是用来读取配置文件(可以是.conf, ...
- 【Luogu】P3865ST表模板(ST表)
题目链接 本来准备自己yy一个倍增来着,然而一看要求O1查询就怂了. ST表模板.放上代码. #include<cstdio> #include<cstdlib> #inclu ...
- Cstring中GetBuffer()方法的主要作用
摘自:http://bbs.csdn.net/topics/310247836 GetBuffer()主要作用是将字符串的缓冲区长度锁定 CString::GetBuffer有两个重载版本: (1 ...
- ubuntu mysql安装及需要其他主机连服务器mysql时的设置(error:10061)
说明: 一个朋友在使用ubuntu-server 16.04安装mysql,设置远程访问的时候出现了问题,请我帮忙.但是,我也没有使用过ubuntu安装mysql,于是乎搜索了很多技术文件,比着葫芦画 ...