【LeetCode】139 - 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".
Solution:
dppos[i]==true/false表示字符串从开头到i的子串是否存在cut方案满足条件
动态规划设置初值bpos[0]==true
string.substr(int beginIndex, int length): 取string从beginIndex开始长length的子串
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) { //runtime:4ms
vector<bool> dppos(s.size()+, false);
dppos[]=true;
for(int i=;i<dppos.size();i++){
for(int j=i-;j>=;j--){ //从右到左找快很多
if(dppos[j]==true && wordDict.find(s.substr(j,i-j))!=wordDict.end()){
dppos[i]=true;
break; //只要找到一种切分方式就说明长度为i的单词可以成功切分,因此可以跳出内层循环
}
}
}
return dppos[s.size()];
}
};
【LeetCode】139 - Word Break的更多相关文章
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 【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】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- 【LeetCode】79. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】127. Word Ladder 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...
随机推荐
- Mybatis Interceptor 拦截器原理 源码分析
Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的),由于插件会深入到Mybatis的核心,因此在编写自己的插件前最 ...
- jsp页面变量作用域问题
想实现一个注销的功能,在页面上有个注销按钮,我想一点它就注销,用了js给按钮加了onclick代码,如下 <% session = request.getSession(true); ...
- js 监听监键盘动作
浏览器firefoxfunctionoperamicrosoftmozilla 转载自:http://geelong.javaeye.com/blog/810054 主要分四个部分第一部分:浏览器的按 ...
- cocos2dx Android 环境搭建 以及 ndk调试
最近在学习cocos2dx,真的很强大,使我们更专注于游戏趣味,免去了繁琐的底层框架代码. cocos2dx的最强大之处当然在于跨平台.跨平台首选当然是Android,好记性不如烂笔头,记下本文分享给 ...
- Java关键字static、final
static 1. static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...
- eclipse有生成不带参数的构造方法的快捷键吗
你打上类名的2个字母,然后”alt“ +“/” 基本上选第一个就行了
- 【转】PWM占空比和分辨率
占空比是接通时间与周期之比冲量相等而形状不同的窄脉冲加在具有惯性的环节上时,其效果基本相同占空比:就是输出的PWM中,高电平保持的时间与该PWM的时钟周期的时间之比,如一个PWM的频率是1000Hz, ...
- php如何控制用户对图片的访问 PHP禁止图片盗链(转载)
把images目录设置成不充许http访问(把图片目录的:读取.目录浏览 两个权限去掉). 用一个PHP文件,直接用file函数读取这个图片.在这个PHP文件里进行权限控制. apache环境中,在你 ...
- (转载)C语言 数组与指针的区别
1) 字符串指针变量是个变量,指向字符串的首地址:而字符串数组名是个常量,为字符串数组第一个元素的地址: 2)字符串指针变量可以赋值,而字符串数组名不能赋值:对于字符数组只能对各个元素赋值,不能用以下 ...
- jquery总结(1)
jquery是一种js对象.里面封装了一些方法,但是jquery对象不能直接使用js方法,js对象不能直接使用jquery方法. jquery对象类似于js对象的集合,就是存在形式是以特殊数组的形式: ...