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".

法I: DP, 二位数组。用所求值“——是否可以被分段,作为状态。

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
//dp[i][j]: s[i...j] can be egemented in dict
//dp[i][j] = dp[i][k] && d[k][j]
int len = s.length();
vector<vector<bool>> dp(len,vector<bool>(len,false));
for(int i = ; i < len; i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()) dp[][i]=true;
for(int j = ; j <= i; j++){
if((wordDict.find(s.substr(j,i-j+))!=wordDict.end()) && dp[][j-]) dp[][i]=true;
}
}
return dp[][len-];
}
};

法II:发现只用到了dp[0][i], i = 0...len-1=>使用一维数组。

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
//dp[i]: s[0...i] can be egemented in dict
//dp[i] = dp[0][k] && d[k][i]
int len = s.length();
vector<bool> dp(len,false);
for(int i = ; i < len; i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()) dp[i]=true;
for(int j = ; j <= i; j++){
if((wordDict.find(s.substr(j,i-j+))!=wordDict.end()) && dp[j-]) dp[i]=true;
}
}
return dp[len-];
}
};

139. Word Break (String; DP)的更多相关文章

  1. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  2. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  3. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  4. 139. Word Break(动态规划)

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  5. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  6. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

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

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

  8. leetcode 139. Word Break ----- java

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  9. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

随机推荐

  1. [转]NSIS:使用SectionSetFlags根据不同环境自动勾选特定区段

    转自: http://www.flighty.cn/html/bushu/20140526_232.html   在微软SQL2000+SP4集成安装版安装包中可以根据目标操作系统自动勾选对应的版本, ...

  2. python 使用selenium和requests爬取页面数据

    目的:获取某网站某用户下市场大于1000秒的视频信息 1.本想通过接口获得结果,但是使用post发送信息到接口,提示服务端错误. 2.通过requests获取页面结果,使用html解析工具,发现麻烦而 ...

  3. call和apply,bind的区别专讲

    可以干什么? 改变函数内的this指向: 什么时候使用? 构造函数使用this 为什么使用? 为了生成对象 类(函数名不可以带括号).call()      因为this指向对象,所以call的第一个 ...

  4. linux read 系统调用剖析

    https://www.ibm.com/developerworks/cn/linux/l-cn-read/ MT注:原文图1与Understanding the Linux Kernel, 3rd ...

  5. 查看linux服务器CPU数量

    首先,要区分两个概念:物理CPU和逻辑CPU. 物理CPU就是服务器上实际安装的CPU.但是一个物理CPU可以有多个核.例如,一个 i5 760 是双核,而一个 i5 2250 是四核.如果开启了In ...

  6. 解决Sybase PowerDesigner 数据库设计中 Name 自动填充Code

    在使用 Sybase PowerDesigner 进行数据库设计时,为了理清思路,需要将name改为中文名称,但是这个软件会自动将name填 充为code,可以通过如下配置修改: 选择tools-&g ...

  7. 经典算法 Manacher算法详解

    内容: 1.原始问题   =>O(N^2) 2.Manacher算法   =>O(N) 1.原始问题 Manacher算法是由题目“求字符串中长回文子串的长度”而来.比如 abcdcb 的 ...

  8. 5.用 CSS 创作一个立体滑动 toggle 交互控件

    原文地址:https://segmentfault.com/a/1190000014638655 HTML代码: <html> <head> <link rel=&quo ...

  9. linux7系统开机报错failed to start login service

    1.开机报错failed to start login service 参考网站:https://unix.stackexchange.com/questions/264994/kali-sudden ...

  10. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...