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, ...
随机推荐
- Android系统之灯光系统--通知灯深入分析
Android通知灯的深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 getSystemService(通知服务) 构造notification 类别 其他参数(颜色,onMS,of ...
- 通过oledb驱动读取excel、csv数据丢失解决方案
1.问题出现 在开发应用程序的过程中,比较常用一功能就是通过oledb驱动读取excel.csv.text等文件:而最近有客户反映,在使用短信平台(下载地址:http://www.sms1086.co ...
- JLOI2015 解题报告
JLOI2015 真的不愧是NOI出题组出的,题目难度够吊.不过每一道都是结论题和乱搞题真的很不好玩... T1:[JLOI2015]有意义的字符串 首先贴下popoqqq的blog吧 感性的认识就是 ...
- ASP.NET使用WebApi接口实现与Android客户端的交互(图片或字符串的接收与回传)
最近在使用WebApi 做下记录 //此接口实现接收Android客户端上传的JSON格式的信息,并返回"nihao"字符串 [Route("ReceiveData& ...
- JAVA三大特性之三——多态
作为JAVA的三大特性之一,多态性是很多人都没有弄清楚的一个重要特性,今天我就来从我所理解的角度来说一下. 首先,从他的字面意思来理解,多态,从其字面来理解就是多种形态,多种表现形式.根据这些,我最能 ...
- 【转】Spring源码编译
原文地址: http://www.flyoung.me/2013/08/02/springcodecompile/ 参考资料: https://github.com/spring-projects/s ...
- 阅读Facebook POP框架 笔记(一)
在这一系列文章里,我主要会将自己阅读第三方代码的经历记录下来,尝试独立分析解剖一个框架.之前也阅读过一些第三方代码,但是实际上来说对自己的成长并没有太大的帮助,因为阅读的不细致,没有领会到代码的精髓. ...
- 【2017-2-17】C#基础 - 定义变量,输入输出
1.初学C#. C#是专门为.NET的应用而开发的语言,他吸收了C++.Visual Basic.Delphi.Java等语言的优点,提高了程序开发的效率. 2.Visual Studio.NET的集 ...
- jQ试题的总结
jQuery习题的一些总结 1.在div元素中,包含了一个<span>元素,通过has选择器获取<div>元素中的<span>元素的语法是? 提示使用has $ ...
- Java Trie树
Tire树,又叫字典树,主要是用来查找单词,词频统计的. 老规矩,直接上代码. package tireTree; public class TireTree { TireNode root; pub ...