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. 杂项:MSMQ

    ylbtech-杂项:MSMQ MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布 ...

  2. Vmware 安装CentOS 6.5

    转自:http://www.centoscn.com/image-text/install/2014/1209/4281.html 其实通过VM安装虚拟机还是蛮简单的,只不过有个别选项可能导致大家安装 ...

  3. IaaS,PaaS,SaaS 的区别和联系

    原文:http://www.ruanyifeng.com/blog/2017/07/iaas-paas-saas.html 越来越多的软件,开始采用云服务. 云服务只是一个统称,可以分成三大类. Ia ...

  4. 2018ICPC网络赛(焦作站)E题题解

    一.题目链接 二.题意 给定一棵树,有四种操作: $1\ u\ v\ x$:把节点$u$到$v$路径上的所有点的权值乘以$x$: $2\ u\ v\ x$:把节点$u$到$v$路径上的所有点的权值加上 ...

  5. 【Python编程:从入门到实践】chapter2 变量和简单数据类型

    2.1 运行2.2 变量 message = "hello" print(message) 2.2.1 变量的命名和使用 2.2.2 使用变量是避免命名错误2.3 字符串 “Hel ...

  6. Arrays.binarySearch 数组二分查找

    public static void main(String[] args) throws Exception { /** * binarySearch(Object[], Object key) a ...

  7. uva-10392-因数分解

    #include<stdio.h> #include<iostream> #include<queue> #include<memory.h> #inc ...

  8. python3基础:字符串、文本文件

    字符串: 练习1: str = "大胖三百磅不是二百磅陪着一百磅的小胖" print(str.replace("磅", "斤")) # 替换 ...

  9. Zabbix监控系统端口

    参考网站: https://www.cnblogs.com/nulige/p/7072019.html

  10. 34. CentOS-6.3安装配置Apache2.2.6

    安装说明 安装环境:CentOS-6.3安装方式:源码编译安装 软件:httpd-2.2.6.tar.gz  | pcre-8.32.tar.gz | apr-1.4.6.tar.gz | apr-u ...