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. 爬虫Larbin解析(二)——sequencer()

    分析的函数: void sequencer() //位置:larbin-2.6.3/src/fetch/sequencer.ccvoid sequencer() { bool testPriority ...

  2. jQuery练习二球队移动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 一些practice和总结(转载)

    转自 http://boundary.cc/2013/05/java-app-server-develop/ by JOKER on 2013/05/05 最近状态不是很好,负能量堆到积爆表,静下心来 ...

  4. ubuntu下启动和关闭tomcat的简单方法

    在ubuntu下面,我们安装tomcat可以有两种方式[1]用aptitude安装aptitude install tomcat6 [2]免安装版从apache tomcat 网站下载apache-t ...

  5. 新环境配置与使用Vim指南

    1.下载源码 git clone git@github.com:vim/vim.git 2.编译 1.安装依赖软件 sudo apt-get install libncurses5-dev libgn ...

  6. JavaScript 高级篇之闭包、模拟类,继承(五)

    本篇主要分享我对闭包的理解及使用闭包完成私有属性.模拟类.继承等,结合大量例子,希望大家能快速掌握!首先让我们先从一些基本的术语开始吧     一.javascript中的闭包 1.我们一起先来理解什 ...

  7. content management system

    Defination of CMS: The definition of a CMS is an application (more likely web-based), that provides ...

  8. 戏(细)说Executor框架线程池任务执行全过程(上)

    一.前言 1.5后引入的Executor框架的最大优点是把任务的提交和执行解耦.要执行任务的人只需把Task描述清楚,然后提交即可.这个Task是怎么被执行的,被谁执行的,什么时候执行的,提交的人就不 ...

  9. ios多手势事件

    开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollVi ...

  10. js数组的声明与应用

    js数组的声明与应用 数组:一种容器,存储批量数据.JavaScript是一种弱类型语言.什么是弱类型,就是变量可以存储多种类型变量的引用不会报错.所以js数组可以存储不同的数据. 一.数组的作用:只 ...