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的更多相关文章

  1. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  3. 【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 ...

  4. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  5. 【LeetCode】916. Word Subsets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...

  6. 【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 ...

  7. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  8. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

随机推荐

  1. .NET单例模式-------各种写法&&验证

    .NET单例模式-------各种写法&&验证 前言 单例模式对大家来说都不陌生,也很容易搞懂其原理,本篇文章也不提供单例模式的详细原理解析,本篇文章的目的是展示在C#中单例模式的各种 ...

  2. IOS系统中使用zepto的live事件绑定不了的一个办法

    IOS系统中使用zepto的live事件绑定不了的一个办法: 对事件对象添加样式:cursor:pointer

  3. R语言---热图的制作

    >install.packages("gplots") > library("gplots")> p <- data.frame(rea ...

  4. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

  5. PHP中对数据库操作的封装

    在动态网面设计中很多都要涉及到对数据库的操作,但是有时跟据需要而改用其它后台数据库,就需要大量修改程序.这是一件枯燥.费时而且容易出错的功作.其实我们可以用PHP中的类来实现对数据库操作的封装,从而使 ...

  6. awk文本处理知识汇总

    参考资料:http://man.linuxde.net/awk http://www.cnblogs.com/chengmo/archive/2013/01/17/2865479.html http: ...

  7. 《Linux/Unix系统编程手册》读书笔记4

    <Linux/Unix系统编程手册>读书笔记 目录 第7章: 内存分配 通过增加堆的大小分配内存,通过提升program break位置的高度来分配内存. 基本学过C语言的都用过mallo ...

  8. Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一)

    相关的maven的 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  9. ubuntu 14.04 与 CentOS 升级GCC/G++至5版本

    # 支持 ubuntu 14.04 add-apt-repository ppa:ubuntu-toolchain-r/test && apt-get update apt-get i ...

  10. shell 块注释

    方法一 : ' 被注释的多行内容 ' 方法二 :<<eof 被注释的多行内容 eof 方法三 :<<! 被注释的多行内容 ! 方法四 if false ; then 被注释的多 ...