LeetCode题解:(139) Word Break
题目说明
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
题目分析
这道题的意思是判断字符串能否由字典里的单词组成,一开始只是以为将字符串分成两部分就可以,进行一些样例测试后才发现可以拆成任意数量的单词,只好重写。
个人思路很简单:
- 将字典里的单词一一和字符串进行比对,将符合的索引区间存储在map里,这样就把字典的单词转化成了不同的区间信息;
- 建立长度为字符串size+1的bitmap,将第0位置为1,并开始遍历bitmap,做如下操作:假如某一位为1,将所有左端为该位的区间的右端在bitmap中置为1;否则直接跳过。
以下为个人实现(C++ 4ms):
class Solution {
public:
map<int, vector<int>> intervals;
void addInterval(string s, string word) {
int left, right;
if (word.size() == 0 || s.size() == 0) return;
for (int i = 0; i < s.size(); i++) {
if (s[i] == word[0]) { // hit first letter
int j;
for (j = 1; i + j < s.size() && j < word.size() && s[i + j] == word[j]; j++); // check
if (j == word.size()) { // match!
intervals[i].push_back(i + j);
}
}
}
}
bool wordBreak(string s, vector<string>& wordDict) {
bool bitmap[s.size() + 1] = {true};
for (int i = 0; i < wordDict.size(); i++) {
addInterval(s, wordDict[i]);
}
for (int i = 0; i < s.size(); i++) {
if (bitmap[i]) {
for (int j = 0; j < intervals[i].size(); j++) {
bitmap[intervals[i][j]] = true;
}
}
}
return bitmap[s.size()];
}
};
LeetCode题解:(139) Word Break的更多相关文章
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】139 - Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode】140. Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
随机推荐
- Cannot find an exact (case-sensitive) match for 'crtbp.m
http://www.ilovematlab.cn/forum.php?mod=viewthread&tid=277326&page=1&extra=#pid3296048
- Ubuntu14.04 64位机上安装OpenCV2.4.13(CUDA8.0)版操作步骤
Ubuntu14.04 64位机上安装CUDA8.0的操作步骤可以参考http://blog.csdn.net/fengbingchun/article/details/53840684,这里是在已经 ...
- 从零开始自学 Java Web
目录: 1.Java JDK下载安装及配置 2.eclipse下载与安装并测试 3.eclipse快捷键 4.Tomcat 下载与安装 5.Tomcat部署Web应用 6.Eclipse中配置Tomc ...
- [webpack]-webpack超级详细搭建实用前端环境
前言: webpack 超级实用前端环境搭建 一.我们日常使用的前端开发环境应该是怎样的? 构建我们需要发布的html,css ,js 文件 使用css 预处理器来编写样式 处理压缩图片 使用Babl ...
- 用Micro:bit播放生日快乐歌
Micro:bit自带一个有趣的功能就是可以生成音乐播放,今天做一个简单实用的案例,用Micro:bit播放生日快乐歌. 算法: 按下按键A,显示生日快乐 播放D 播放D 播放E 播放D 播放G 播放 ...
- Python字符串符号:双引号/单引号用法注解。
众所周知python中单引号和双引号常常被我们所使用,例如print.input等等. 但是对于打印输出所引导的字符串大多都是用双引号的形式来做,"Hello,python!",而 ...
- LeetCode 刷题笔记 2. 有效的括号(Valid Parentheses)
tag: 栈(stack) 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须 ...
- 环境变量的配置-java-JMETER - 【Linux】
rz上传 lz下载 步骤: . Linux下首先安装Jdk: . 下载apache-jmeter-4.0.tgz,复制到Linux系统中的/opt目录下: . 解压apache-jmeter-4.0. ...
- PHPCMS增加SEO字段调用
alter table v9_site add site_title_index varchar(255) not null;alter table v9_site add keywords_ind ...
- jquery 3.0 新版本
https://code.jquery.com/