【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 ...
随机推荐
- Android 软键盘弹出时把布局顶上去,控件乱套解决方法
解决办法:方法一:在你的activity中的oncreate中setContentView之前写上这个代码getWindow().setSoftInputMode(WindowManager.Layo ...
- Ajax动态滚动加载数据
看新浪微博,人人网都有这样的效果:滚动条滚动到最下面的时候,新的数据就被自动加载出来了,今天亲自尝试了一下这个效果的实现. 最开始在CSDN上写了一版,功能比较简单,今天又增加了一个小功能:翻页到指定 ...
- RTC系统【转】
http://blog.csdn.net/fanqipin/article/details/8089995 转自:http://www.cnblogs.com/muhuacat/p/5276306.h ...
- 通过从代码层面分析Linux内核启动来探知操作系统的启动过程
通过从代码层面分析Linux内核启动来探知操作系统的启动过程 前言说明 本篇为网易云课堂Linux内核分析课程的第三周作业,我将围绕Linux 3.18的内核中的start_kernel到init进程 ...
- dojo 一 require 路径问题
dojo.baseUrl baseUrl用来存储dojo.js存放 的跟目录,例如dojo.js的路径是“/web/scripts/dojo-1.3/dojo/dojo.js”则baseUrl为“/w ...
- python公司面试题集锦 python面试题大全
问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...
- How do I install uudecode?
uudecode is in the package sharutils Install it with the package manager or start a terminal and typ ...
- [CFgym101061C]Ramzi(贪心,双条件最短路)
题目链接:http://codeforces.com/gym/101061/problem/C 题意:一张图,图上的边有两种,一种是车道,一种是人行道.一个人要从A点到B点,可以坐车也可以走人行道.这 ...
- XML中对特殊字符的处置
str = str.replaceAll("‘", "‘"); str = str.replaceAll("’", "‘" ...
- HDFS常用命令
HDFS 常用的文件操作命令 hdfs dfs -text /pub/20151019/1/4/gwmvod/mediags.moretv.com.cn/*.bz2 | wc -l hdfs dfs ...