Word Break - LeetCode
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".
思路:DP。
维护一个一维bool类型数组DP[]。DP[i]表示字符串S从第0位到第i位子串是否能够分割成字典里的单词。
然后遍历一遍字典,记录下字典中单词的最大长度和最小长度,分别记为max_l和min_l。
之后进行二重循环,外层循环枚举的是单词在S中的结束位置,因为已知最短的单词长为min_l,设S长为n,则枚举的范围是min_l - 1到n - 1。
内层循环枚举的是单词的开始位置,即以外层循环所枚举的结束位置为基准,向前找min_l到max_l的距离,更近或更远都不可能存在单词了。
这里注意一个点,内层循环在枚举了一个开始位置后,设其为i,若i不为0,则判断下dp[i - 1]是否为true,若为false则直接continue。因为若前面都无法由单词构成,则这里就算由单词构成也没什么用。
最后dp[n - 1]即为结果。
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int n = s.size();
if (n == ) return false;
vector<bool> dp(n, false);
unordered_set<string>::iterator iter = wordDict.begin();
unordered_set<string>::iterator end = wordDict.end();
int max_l = INT_MIN;
int min_l = INT_MAX;
for (; iter != end; iter++)
{
int size = iter->size();
max_l = max(max_l, size);
min_l = min(min_l, size);
}
for (int i = min_l - ; i < n; i++)
for (int j = i - min_l + ; j >= && j >= i - max_l + ; j--)
{
if (j - >= && !dp[j - ]) continue;
string word = s.substr(j, i - j + );
if (wordDict.count(word) > ) dp[i] = true;
}
return dp[n - ];
}
};
Word Break - LeetCode的更多相关文章
- Word Break leetcode java
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
随机推荐
- greenplum-时间处理
工作中遇到,需要改变两周以前的数据状态,于是查了下资料,原来数据库直接就可以处理,所以分享给大家! 在PostgreSQL中可以直接对时间进行加减运算:. SELECT now()::timestam ...
- NGUI 学习总结
NGUI 学习一段时间了,这里总结一下,用于以后查看. 获取组件 在Awake函数里获取组件,然后就可在Start以及其他函数里使用 lbl = GetComponent<UILabel> ...
- FastDFS的安装(复制自己用)
FastDFS 安装及使用 FastDFS 安装及使用 2012-11-17 13:10:31| 分类: Linux|举报|字号 订阅 Google了一下,流行的开源分布式文件系统有很多,介 ...
- 团队Alpha版本冲刺(一)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...
- javascript原生360 开机小动画
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- POJ 1149 PIGS(Dinic最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20738 Accepted: 9481 Description ...
- HDFS设计思想
HDFS设计思想 DataNode:用来在磁盘上存储数据 HDFS 数据存储单元( block ) 1 文件被切分成固定大小的数据block块 •默认数据块大小为 64MB(hadoop1.x版本6 ...
- css选择器位置和数量技巧
1. 除去首个元素 li:not(:first-child) li + li li:first-child ~ li 2. 第1-3个元素 li:nth-child(-n+3) 3. 除去第1-3个元 ...
- Codeforces Round #323 (Div. 2) B 贪心,暴力
B. Robot's Task time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- nodejs + express 入门之 hello world
使用nodejs 开发web项目时原生的api开发比较困难.组合express后就比较容易了. 1.安装 express: (express地址: http://www.expressjs ...