[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 ...
随机推荐
- 深入理解shared pool共享池之library cache的library cache pin系列三
关于library cache相关的LATCH非常多,名称差不多,我相信一些人对这些概念还是有些晕,我之前也有些晕,希望此文可以对这些概念有个更为清晰的理解,本文主要学习library cache p ...
- 7-1 DBA顾问培训内容@20141230
1, 逻辑读还是物理读? 查询语句的实际执行计划. F5 预计执行计划. --如何产生实际执行计划 ??. --Session收集指令. workload repository report fo ...
- 制作Net程序的帮助文档--总结
一.工具的准备 目前,一般采用Sandcastle Help File Builder工具来制作.Net程序帮助文档,该工具主要是利用Xml文档里的信息以及DLL文件来生成完整的帮助文档.在Visua ...
- 设计模式之 Singleton 单例模式
先上两段代码,区别仅在于是否涉及线程安全. 首先是不涉及多线程的单例: public class Singleton { private final static Singleton INSTANCE ...
- [Machine Learning] Probabilistic Graphical Models:一、Introduction and Overview(2、Factors)
一.什么是factors? 类似于function,将一个自变量空间投影到新空间.这个自变量空间叫做scope. 二.例子 如概率论中的联合分布,就是将不同变量值的组合映射到一个概率,概率和为1. 三 ...
- css3学习--css3动画详解二(3d效果)
一.设置3D场景 perspective:800 //浏览器到物体的距离(像素)perspective-origin:50% (x轴) 50% (y轴) //视点的位置 transf ...
- sublime text下载和汉化
好处就不说了,能认识到这款编辑器,基本上对它有一定的了解了. Sublime Text2是一款开源的软件,不需要注册即可使用(虽然没有注册会有弹窗,但是基本不影响使用). 官方网站:http://ww ...
- Python核心编程2第五章课后练习
5-1 整型,讲讲python普通整型与长整型区别 python整形一共有三种:布尔型,长整型和标准整型.普通整型与长整型的区别在于标准整形的取值范围是-2^31到2^31-1,长整型所能表达的数值与 ...
- typedef 类型重命名 和 #define 宏定义(1)
http://www.blogjava.net/jasmine214--love/archive/2010/11/29/339307.html 在现实生活中,信息的概念可能是长度,数量和面积等.在C语 ...
- 轻量级表格插件Bootstrap Table。拥有强大的支持固定表头、单/复选、排序、分页、搜索及自定义表头等功能。
Bootstrap Table是轻量级的和功能丰富的以表格的形式显示的数据,支持单选,复选框,排序,分页,显示/隐藏列,固定标题滚动表,响应式设计,Ajax加载JSON数据,点击排序的列,卡片视图等. ...