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的更多相关文章

  1. Word Break leetcode java

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

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

  3. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  4. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  5. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  6. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  7. 【leetcode】Word Break (middle)

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

  8. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

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

随机推荐

  1. OpenCV学习笔记(六) 滤波器 形态学操作(腐蚀、膨胀等)

    转自:OpenCV 教程 另附:计算机视觉:算法与应用(2012),Learning OpenCV(2009) 平滑图像:滤波器 平滑 也称 模糊, 是一项简单且使用频率很高的图像处理方法.平滑处理的 ...

  2. mysql破解密码安装与基本管理

    一.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...

  3. navicat常用快捷键及注意事项

    常用快捷键: 1. ctrl + q: 打开新查询窗口 2. ctrl + r: 运行当前窗口内的所有语句 3. ctrl + w: 关闭当前窗口 4. F6: 打开一个mysql命令行窗口 ---- ...

  4. Java集合中的细节问题

    1)集合不保存基本数据类型,而是会把基本数据类型装箱后保存. 2)Empty和null的区别:null是不存在,Empty已经初始化了,只不过里面是空的. 3)判断集合有效性: 先判断空,再判断emp ...

  5. Spring Boot 学习系列(03)—jar or war,做出你的选择

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 两种打包方式 采用Spring Boot框架来构建项目,我们对项目的打包有两种方式可供选择,一种仍保持原有的 ...

  6. NOS直传加速服务

    本文来自网易云社区 作者:孙建良 最近团队在对存储系统做一些性能测试,期间遇到了不少问题,测试过程中得出的结果也没有很好的数据支撑,所以尝试了非常多的方法来对性能问题进行定位. 小王童鞋是挺厉害的,使 ...

  7. Python框架之Django学习笔记(六)

    模板 上篇博文学习了动态视图,但是,视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. def current_datetime(request): now = ...

  8. VMware Fusion Pro安装Ubuntu 18.04.1

  9. CentOS7 'Username' is not in the sudoers file. This incident will be reported

    新装的 CentOS 需要安装许多软件,但是如果一开始你不是以 root 登入的话,就需要使用 sudo 进行切换,但是通常会报错如下图: 解决方法: 先用 root 用户登入系统, 打开文件 vi ...

  10. Normal synchronous FIFO mode 和 Show-ahead synchronous FIFO mode

    FIFO是先进先出,可以用fifo来处理跨时钟域的数据传输问题,用到的地方特别多,一定要搞会. 在学习调用fifo的IP核中发现有normal synchronous FIFO mode 和 Show ...