[LeetCode] Word Break 解题思路
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".
解题思路:
问题: 根据给定的单词字典,判断一个字符串是否可以全部被分割为有效单词。
第一个问题,给定一个字符串, 怎么判断它是不是一个有效单词呢?
本人首先想到是之前做过的 Trie Tree ,打算用 Trie Tree 结构来判断一个字符串是不是一个有效单词。
后来才知道, unordered_set 是 C++ 自带的数据结构,能直接用来检索一个字符串是否为有效单词。囧。看来对 C++ 还不够熟悉。
值得一提的是,用 Trie Tree 实现的算法也通过了 LeetCode 的测试,当然,unordered_set 的完成效率更快。
第二个问题,如何判断一个字符串是否可以全部被分割为有效单词,即原问题。
假设将字符串 s 分割为两段,[0,i-1], [i, n-1],如果[0, i-1] 为有效单词,[i, n-1]为有效单词集合,那么 s 就是一个有效字符串。
将 i 从 1 到 n-1 遍历一次,求得 s 是否是一个有效字符串。
第三个问题,效率满,出现很多反复求解的子问题。
DP思路,即表格法,记录已计算结果。
vector<int> v;
bool isMatch(string s, unordered_set<string>& wordDict){
unordered_set<string>::iterator us_iter = wordDict.find(s);
if (us_iter != wordDict.end()) {
return true;
}
for (int i = ; i < s.size(); i++) {
string leftS = s.substr(, i);
unordered_set<string>::iterator leftS_iter = wordDict.find(leftS);
if (leftS_iter != wordDict.end()) {
bool isWordR;
if (v[i] != -) {
isWordR = v[i];
}else{
isWordR = isMatch(s.substr(i, s.size() - i), wordDict);
v[i] = isWordR;
}
if (isWordR) {
return true;
}
}
}
return false;
}
bool wordBreak(string s, unordered_set<string>& wordDict) {
vector<int> tmp(s.size(), -);
v = tmp;
bool res = isMatch(s, wordDict);
return res;
}
[LeetCode] Word Break 解题思路的更多相关文章
- [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 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [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 && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- 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]Word Break @ Python
原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...
随机推荐
- 重温sql语句中的join操作
1.join语句 Sql join语句用来合并两个或多个表中的记录.ANSI标准SQL语句中有四种JOIN:INNER,OUTER,LEFTER,RIGHT,一个表或视图也可以可以和它自身做JOIN操 ...
- Masonry等比缩放
第一种: CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]); CGFloat aspectRatio = 1 ...
- C++关于编译器合成的默认构造函数
有两个常见的误解: 1.任何类如果没有定义默认构造函数,就会被合成出一个来. 2.编译器合成的默认构造函数会显式地设定类内每一个数据成员的默认值. 对于第一个误解,并不是任何类在没有显式定义默认构造函 ...
- STP根交换机,指定端口,根端口,阻塞端口
STP判断各交换机端口(指定端口:DP;根端口:RP;阻塞端口:AP) 判断步骤: 1.选举根交换机: 判断对象:所有运行STP协议的交换机中选出一个; 判断依据:交换机中选择网桥ID值(交换机优先级 ...
- [学习笔记]设计模式之Command
为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 在上篇Chain of Responsibility(职责链)模式笔记中,我们学习了一种行为型设计模式.今天,我们继续这一主题,来学习 ...
- html5 拖拽的简要介绍
1,首先,你要告诉计算机那个元素可以拖动,或者是一张图,或者是一个盒子,在标签里面加上 draggable="true" 2,然后,监听该元素被拖动的函数 ondragstart ...
- js学习--DOM操作详解大全一(浏览器对象)
一.客户端中的window对象 window对象表示当前浏览器的窗口,它是一个顶级对象,我们创建的所有对象.函数.变量都是window对象的成员. window对象自带了一些非常有用的方法.属性. w ...
- wordpress4.0.1源码学习和摘录--函数
1.根据类型获取当前时间 function current_time( $type, $gmt = 0 ) { switch ( $type ) { case 'mysql': return ( $g ...
- 苹果搜索广告后台大揭秘,最全最细致详解,手把手设置教程「后附官方视频」-b
WWDC2016 搜索广告分会视频和 PPT 发布了,ASO100 带开发者第一时间了解 Search Ads 后台设置(文末有原声视频). 首先介绍一下搜索广告的模式和竞价规则 广告模式为 CPT( ...
- jsonp跨域问题记录
这段时间用H5做移动app开发,遇到不少之前做web的时候不曾遇到的问题,记录一下,共勉-- 首先说一个:js跨域取数的问题 描述: 之前做web都是通过后台获取数据,没考虑过跨域的问题.这次用h5 ...