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 ...
随机推荐
- Android Kotlin适用小函数
都是一些Android适用的Kotlin小函数. 1.点击空白隐藏键盘 //点击空白隐藏键盘 override fun onTouchEvent(event: MotionEvent): Boolea ...
- loj2173 「FJOI2016」建筑师
ref 真是道组合数学神题啊--第一次见第一类斯特林数-- #include <iostream> #include <cstdio> using namespace std; ...
- mysql中为int设置长度究竟是什么意思
根据个人的实验并结合资料:1.长度跟可以使用的值的范围无关,值的范围仅跟类型对应的存储字节数和是否unsigned有关:2.长度指的是显示宽度,比如,指定3位int,那么id为3和id为300的值,在 ...
- IOS开发---菜鸟学习之路--(八)-实现新闻页面
本章将具体讲述如何结合前两张的内容最终实现一个新闻页面的雏形 之所以称之为雏形,是因为本章实现的内容只是实现了最基础的效果 还有很多其他诸如下拉刷新 页面导航等效果都需要投入一些时间进行研究 好了直接 ...
- 设计模式之迭代器模式 Iterator
代码实现 public interface MyIterator { void first(); //将游标指向第一个元素 void next(); //将游标指向下一个元素 boolean hasN ...
- springmvc和struts2的区别比较
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...
- 系统中同时安装sql2005 和 sql2008 R2 提示要删除SQL Server 2005 Express
修改注册表:HKLM\Software\Microsoft\Microsoft SQL Server\90\Tools\ShellSEM,把 ShellSEM重命名即可 如果是64位机器 在 HKL ...
- python 字符编码与转码
一. 字符编码 ASCII: 一个字节,最多能表示255个字符 GB2312(1980年):一共收录了7445个字符,包括6763个汉字和682个其它符号. GBK1.0(1995年):收录了2188 ...
- RESTful-rest_framework认证组件、权限组件、频率组件-第五篇
认证组件.权限组件.频率组件总结: 认证组件格式: 1 写一个认证类 from rest_framework.authentication import BaseAuthentication cla ...
- 实战小项目之嵌入式linux图像采集与传输
项目简介 本次编程实战主要是围绕嵌入式linux v4l2采集框架展开,包括以下几个部分: v4l2视频采集 IPU转码 framebuffer显示 自定义UDP简单协议进行传输 上位机软件 ...