单词拆分 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 ...
随机推荐
- 牛逼的lsof命令!!!
linux lsof命令详解 简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访 ...
- PostMan 使用Interceptor 发送带cookie的请求一直loading
问题 最近要写一个爬虫(虽然是第一次写),于是就用了Chrome上非常方便一个插件,PostMan,但是由于chrome安全的限制,发不出带cookie和带有自定义头部标签的请求. 百度一番后得如果想 ...
- 小朋友学C++(2)
多态 (一) 先编写函数: #include <iostream> using namespace std; class Shape { protected: int width, hei ...
- js数组的初始化
方法一: var myarray = new Array(66,80,90,77,59); 方法二: var myarray = [66,80,90,77,59]; 方法三: var myarray= ...
- Java面向对象技术
问题及答案来源自<Java程序员面试笔试宝典>第四章 Java基础知识 4.2面向对象技术 1.面向对象与面向过程有什么区别? 看下面一个实例即可: 面向过程就是分析出解决问题所需要的步骤 ...
- 0_Simple__simpleCooperativeGroups
▶ 协作组,CUDA9.0 的新特性 ▶ 源代码,如何获得协作组的编号? #include <stdio.h> #include "cuda_runtime.h" #i ...
- win2008安装IIS
win2008安装IIS http://jingyan.baidu.com/article/fec4bce2398747f2618d8b88.html http://127.0.0.1/ 新建网站,端 ...
- 在客户端浏览器中点击下载生成excel
生成excel的样式,里面的数据已经写好,使用apache,poi来写的. 1.首先是controller /** *下载服务结构体Excel * *@return */ @RequestMappin ...
- vector实现(只能装入string)
#include<iostream> #include<string> #include<memory> #include<utility> using ...
- mongodb基础学习14-mapReduce操作
mapReduce随着大数据的兴起而流行,相当于传统数据库的group操作,强项在于分布式计算. map:将一组记录的相关信息映射到一个数组 reduce:对map得到的数组数据进行处理得到一个结果 ...