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

==================== 暴力法、穷尽法 ====================

【思路】

这道题事实上是水过的。

我直接的想法是,从s的第一个字母開始,逐个添加字母去dict里找,假设找到,就继续从下一个位置開始,逐个添加字母去dict里找。假设没找到。就返回false。可是这样算法不争取,比方 s="aaaaaaa",dict=["aaa", "aaaa"],按上面算法返回false,但实际上是能够切割成两个单词的。

错误的原因非常easy。有漏掉情况。于是我想到採用暴力的方法。 把全部可能的组合都找出来,仅仅要有组合成功的就返回true。代码例如以下:

class Solution {
boolean ret; public boolean wordBreak(String s, Set<String> dict) {
String[] all = dict.toArray(new String[0]);
ret = false;
nextWord(0, s, all);
return ret;
} void nextWord(int pos, String s, String[] all) {
if (ret) return;
if (pos == s.length()) ret = true; for (int i = 0; i < all.length; i++) {
if (s.indexOf(all[i], pos) == pos) {
nextWord(pos + all[i].length(), s, all);
}
}
}
}

可是,这样会超时。LeetCode给出了超时的例子:

s=“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab”

dict=["a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa"]

我一看,重点是最后哪一个b,于是我想到了,遍历s中的全部字符,看看有没有在dict中没有出现的,假设dict中全部单词都不含这个字符,那么s肯定是不可分解的。

基于此我添加了部分代码:

【Java代码】

class Solution {
boolean ret; public boolean wordBreak(String s, Set<String> dict) {
String[] all = dict.toArray(new String[0]); //////////////////////////////////////////////////
//假设s中有字母没在dict出现过,那么结果肯定为false
for (int i = 0; i < s.length(); i++) {
boolean flag = false;
for (int j = 0; j < all.length; j++) {
if (all[j].indexOf(s.charAt(i)) > -1) {
flag = true;
break;
}
}
if (!flag) {
return false;
}
}
////////////////////////////////////////////////// ret = false;
nextWord(0, s, all);
return ret;
} void nextWord(int pos, String s, String[] all) {
if (ret) return;
if (pos == s.length()) ret = true; for (int i = 0; i < all.length; i++) {
if (s.indexOf(all[i], pos) == pos) {
nextWord(pos + all[i].length(), s, all);
}
}
}
}

说这道题是水过的就是由于LeetCode有提示不通过例子。这样我就能依据例子输入输出来改动代码。

【LeetCode】Word Break 解题报告的更多相关文章

  1. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  4. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

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

  6. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  7. [LeetCode] Word Break II 解题思路

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

  8. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

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

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

随机推荐

  1. GloVe词分布式表示

    GloVe 模型介绍 下面的内容主要来自https://blog.csdn.net/u014665013/article/details/79642083 GloVe的推导 GloVe是基于共现信息来 ...

  2. Ubuntu 深度炼丹环境配置

    Ubuntu 深度炼丹环境配置 深度炼丹最麻烦的就是环境配置了,下面过程记录了本人进行深度炼丹环境的配置. 首先是安装图形显卡驱动,按如下指令进行即可 sudo add-apt-repository ...

  3. 【转载】logistic回归

    原文地址:https://www.cnblogs.com/zichun-zeng/p/3824745.html 1. logistic回归与一般线性回归模型的区别: (1)     线性回归的结果变量 ...

  4. 2015长春网络赛1001 求连通快数量的问题dfs

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  5. php默认有最大执行时间

    执行php默认有最大执行时间,默认30s,修改,不能设置’1h’,貌似单位不能修改

  6. 正则表达式的\b与\B总结

    \b 单词边界,是指单词与符号之间的边界,是一个位置,不是空格或字符.(这里单词可以是中文字符,英文字符,数字:  符号可以是中文符号,英文符号,空格,制表符,换行).不能与量词?+*{1}{2,5} ...

  7. jquery工具方法总结

    $.extend 对象合并,支持深拷贝 $.each 相当于array.each或object.each,可以遍历数组和对象 $.grep 相当于array.filter $.map 相当于array ...

  8. FOJ Problem 2253 Salty Fish

                                                                                                        ...

  9. polyfill for Function--源码

    /** * polyfill for Function */ // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer ...

  10. Android 4.4 不休眠+不锁屏+默认中文+去除导航栏

    1.不休眠 frameworks/base/packages/SettingsProvider/res/values/defaults.xml 里面60000改成-1,就是不进入休眠. 这个文件还保存 ...