[抄题]:

给出一个字符串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在词典中

[一刷]:

  1. 布尔型can数组的长度是字符串长度+1,不是词典数+1

[二刷]:

  1. i <= s.length() 表示最后一位边界也要处理
  2. 取子串的方法是.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的更多相关文章

  1. leetcode 140 单词拆分2 word break II

    单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...

  2. leetcode 139 单词拆分(word break)

    一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...

  3. [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 ...

  4. LeetCode 139. 单词拆分(Word Break)

    139. 单词拆分 139. Word Break

  5. Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode

    这道题相似  Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...

  6. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  7. [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 ...

  8. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

随机推荐

  1. java如何写自己的native方法实现调用本地的c++库?

    等待编辑 1:首先可以找一本jni    java native interface相关的书籍来看.

  2. Linux常见英文报错中文翻译(菜鸟必知)

    Linux常见英文报错中文翻译(菜鸟必知) 1.command not found 命令没有找到 2.No such file or directory 没有这个文件或目录 3.Permission ...

  3. ie6,7下的textarea的type获取

    <input type='button' value="按钮" class='gys'> <textarea class='gys gystextarea'> ...

  4. Oracle直方图的详细解析(转)

    Oracle直方图解析 一.    何谓直方图: 直方图是一种统计学上的工具,并非Oracle专有.通常用于对被管理对象的某个方面的质量情况进行管理,通常情况下它会表现为一种几何图形表,这个图形表是根 ...

  5. PHP流程控制 - if 语句

    PHP - if 语句 if 语句用于仅当指定条件成立时执行代码. 语法 if (条件) { 条件成立时要执行的代码; } 如果当前时间小于 20,下面的实例将输出 "Have a good ...

  6. Kafka 基本原理

    Kafka 基本原理   来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...

  7. angularjs探秘<一>认识angularjs

    首先聊聊angularjs是啥. 首先AngularJS 是一个 JavaScript 框架.(PS:其实就是外部引用的js文件) 所以AngularJS的使用依然是外部引用js文件. 附上引用地址 ...

  8. c#面向对象基础3

    静态与非静态的区别 (1)在非静态类中既可以有实例成员,也可以有静态成员(static修饰). (2)在调用静态成员的时候要使用:对象名.实例成员. (3)在调用静态成员的时候要使用:类名.静态成员. ...

  9. zabbix_get无法执行agent端的脚本文件解决办法

    一,无法执行脚本参考网站:http://blog.51cto.com/13589448/2070180 权限不足时提示: server端提示: [root@yao local]# zabbix_get ...

  10. apo 简单参考

    参考: https://www.cnblogs.com/Geyoung/p/6927905.html @Aspect @Component public class TimeAspect { //通过 ...