LeetCode -- Word Break 动态规划,详细理解
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
【思路】
对于该问题我一开始的做法就是,尽可能匹配,例如 s = "abcdefabc" dict=["abc","def"] 我只要把s 中所有存在于字典中的词语去掉,最后如果s没有任何字母则表示能够break;
但是问题来了,s="aaaaaaa" dict=["aaa","aaaa"],这个时候就会直接用aaa去把s分成 aaa,aaa,a;从而返回false.
再比如,s="abcdeefg" dict=["ab","cde","ee","cd","fg"],当用字典中的"cde"去分割的时候,结果是 ab, cde, e, fg; 从而返回false.
【动态规划解题】

【重点 ★★】
从s[2]=c开始,我们发现了两个字典词语与之相匹配,即:cde,cd,我们标记出他们能拼接的长度
ab cdeefg
ab
cde
cd
--->接下来,我们就从 efg或者eefg的位置开始匹配

【代码】
public class Solution {
public boolean wordBreak(String s, Set<String> dict){
boolean[] t =new boolean[s.length()+1];
t[0]=true;//set first to be true, why?
//Because we need initial state
for(int i=0; i<s.length(); i++){
//should continue from match position
if(!t[i])
continue;
for(String a: dict){
int len = a.length();
int end = i + len;
if(end > s.length())
continue;
if(t[end])continue;
if(s.substring(i, end).equals(a)){
t[end]=true;
}
}
}
return t[s.length()];
}
}
LeetCode -- Word Break 动态规划,详细理解的更多相关文章
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 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] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
随机推荐
- GitHub 上下载代码运行报错 :'The sandbox is not sync with the Podfile.lock\'
问题描述: github下载的Demo,很多时候使用到CocoaPods,有的时候因为依赖关系或者版本问题不能编译运行.出现例如The sandbox is not sync with the Pod ...
- (4)activiti之uel表达式
有了前面几章,我们肯定有一定的困惑,activiti如何与实际业务整合,比如一条采购单,如何跟一个流程实例互相关联起来? 这里就需要使用到activiti启动流程实例时设置一个流程实例的busines ...
- Hadoop1.0.3环境搭建流程
0x00 大数据平台相关链接 官网:http://hadoop.apache.org/ 主要参考教程:http://www.cnblogs.com/xia520pi/archive/2012/05/1 ...
- PHPCMS-后台管理中心
这个就是便捷管理网页,可以通过这个后台进行修改.增删一些东西,还可以利用一些网页模板来建立网页 首先就是下载好这个后台管理中心,这个从网上下载就好了,记住这个要安装在WampServer中的www文件 ...
- Centos更换yum源
Centos更换yum源 步骤如下: 备份原始源 cd /etc/yum.repos.d/ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/ ...
- repeater绑定泛型list<string>
菜鸟D重出江湖,依然是菜鸟,囧!言归正传—— 工作中遇到一个repeater绑定的问题,数据源是一个list<string> 集合,然后在界面上使用<%#Eval()%>绑定. ...
- 2017-2-19 C#基础 数据类型
数据类型分为基本数据类型和引用类型.基本数据类型分为两大类,值类型,字符型(char)和布尔型(bool).其中值类型分为整型和浮点型.整型分为byte,short,int,long.常用的是int( ...
- Ansible详解(一)
简介 Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无宕机更新等).主版 ...
- ABP Zero 多租户管理
ABPZero - 多租户管理 启用多租户 ASP.NET Boilerplate和module-zero可以运行多租户或单租户模式.多租户默认为禁用.我们可以在我们的模块PreInitialize方 ...
- Java设计模式之《组合模式》及应用场景
摘要: 原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6489827.html 组合模式,就是在一个对象中包含其他对象,这些被包含的对象可能是 ...