[抄题]:

给出一个字符串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. [UE4]Character,飞行模式。

    Character 是pawn的子类,可以行走.跳跃.游泳.飞行的Pawn.

  2. js判断各种浏览器

    <script type="text/javascript"> if (navigator.appName.indexOf("Microsoft Intern ...

  3. Ubuntu下Code::Blocks错误

    #error This file requires compiler and library support for the ISO C++ 2011 standard. This support i ...

  4. php笔记篇(二)

    mysql中key .primary key .unique key 与index区别(http://www.manongjc.com/article/1487.html) php is_file() ...

  5. MySQL 创建数据库的两种方法

    使用 mysqladmin 创建数据库 使用普通用户,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用户拥有最高权限,可以使用 mysql mys ...

  6. 【Python量化投资】基于技术分析研究股票市场

    一 金融专业人士以及对金融感兴趣的业余人士感兴趣的一类就是历史价格进行的技术分析.维基百科中定义如下,金融学中,技术分析是通过对过去市场数据(主要是价格和成交量)的研究预测价格方向的证券分析方法. 下 ...

  7. css实现文本两行或多行文本溢出显示省略号

    word-break: break-all; text-overflow: ellipsis; display: -webkit-box; /** 对象作为伸缩盒子模型显示 **/ -webkit-b ...

  8. C宏定义

    宏定义中宏名一般用大写,用以和一般的变量名区分开来,但是用宏名用小写也没有错; 对程序中用双引号括起来的字符串内的字符,不进行宏替换操作, #include<stdio.h> #defin ...

  9. 9.简单理解ajax

    #### post 请求需要发送一个header setRequestHeader('Content-Type','application/x-www-form-urlencoded') post请求 ...

  10. WindowBrush

    m_Element.Fill = SystemColors.WindowBrush; BorderBrush="{x:Static SystemColors.WindowBrush}&quo ...