单词拆分 I · Word Break
[抄题]:
给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。
s = "lintcode"
dict = ["lint","code"]
返回 true 因为"lintcode"可以被空格切分成"lint code"
[思维问题]:
看到字符串就怕:还是要掌握一般规律
[一句话思路]:
还是看rU手册456页的解法吧
前部完美切分+后部是单词
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

"aaaaaaa" ["aaaa","aa"] false。f[2] = f["li"] f[0] f[全部]都是true
lin完美切分+t在词典中
[一刷]:
- 布尔型can数组的长度是字符串长度+1,不是词典数+1
[二刷]:
- i <= s.length() 表示最后一位边界也要处理
- 取子串的方法是.substring()
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
为了取出长度为j的单词,起点其实是i - j

[复杂度]:Time complexity: O(n*l^2+m) Space complexity: O(n^2)
查询字符串是否在词典中也是n
N字符串长度
M单词个数
L单词平均长度
[英文数据结构或算法,为什么不用别的数据结构或算法]:
从右边开始切割,待判断单词由短到长,割到最大单词长度L还没有即可终止。
从左边开始切割,待判断单词由长到短,复杂度太高。

hash map查询单词,长度L不可忽略时,复杂度是L, 可忽略时为1
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
word break2
[代码风格] :

public class Solution {
/*
* @param s: A string
* @param dict: A dictionary of words dict
* @return: A boolean
*/
//maxLength
private int maxLength (Set<String> dict) {
int max = 0;
for (String word : dict) {
max = Math.max(max, word.length());
}
return max;
}
public boolean wordBreak(String s, Set<String> dict) {
//corner case
if (s == null || dict == null) {
return false;
}
//state
//boolean can[] = new boolean[dict.size() + 1];
boolean can[] = new boolean[s.length() + 1];
int max_len = maxLength (dict);
//initialization
can[0] = true;
//function
for (int i = 1; i <= s.length(); i++) {
can[i] = false;
for (int j = 0; j <= i && j <= max_len; j++) {
if (can[i - j] == false) {
continue;
}
String word = s.substring(i - j, i);
if (dict.contains(word)) {
can[i] = true;
break;
}
}
}
//answer
return can[s.length()];
}
}
单词拆分 I · Word Break的更多相关文章
- leetcode 140 单词拆分2 word break II
单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...
- leetcode 139 单词拆分(word break)
一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...
- [Swift]LeetCode140. 单词拆分 II | Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- LeetCode 139. 单词拆分(Word Break)
139. 单词拆分 139. Word Break
- Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode
这道题相似 Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- [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-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
随机推荐
- 亚马逊云VPS AWS更改LINUX为ROOT权限密码登陆
LINUX VPS没有ROOT权限是很难受的事,并且密码登陆也方便一些.首先用AWS证书验证的账户登录,1.修改ROOT密码sudo passwd root2.sudo chmod 777 /etc/ ...
- 如何在CentOS中添加Swap
1.检查 Swap 空间 在设置 Swap 文件之前,有必要先检查一下系统里有没有既存的 Swap 文件.运行以下命令: 1 swapon -s 如果返回的信息概要是空的,则表示 Swap 文件不存在 ...
- MTR追踪的好工具
yum install mtr 或者win下的winmtr 直接可以统计.
- spark 多语言编程
参考官方地址:https://spark.apache.org/docs/1.6.2/programming-guide.html 误解: spark多语言的支持,并不是说spark可以操作各个语言写 ...
- python实现定时发送系列
1.发送邮件实现 2.定时任务实现 3.定时发送邮件实现 4.微信定时发送信息 详细源代码见:https://github.com/15387062910/timing_send 参考: 廖雪峰博客 ...
- JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类
// 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); ...
- [Flutter] TextField 中只允许输入合法的小数
的Flutter的TextField中,我们可以通过设置inputFormatters来过滤和限制输入的内容. 下面是一个自定义的 TextInputFormatter,可以限制用户只能输入有效的整数 ...
- CUDA C Programming Guide 在线教程学习笔记 Part 7
▶ 可缓存只读操作(Read-Only Data Cache Load Function),定义在 sm_32_intrinsics.hpp 中.从地址 adress 读取类型为 T 的函数返回,T ...
- POI 解析excel 空行问题
https://www.cnblogs.com/interdrp/p/4019583.html
- centos的安装和下载
https://blog.csdn.net/risen16/article/details/50737948