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. [git 学习篇] git文件版本回退再学习

    需求;  准备把readme.txt回退到上一个版本,也就是“add distributed”的那个版本 首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3 ...

  2. oracle无参数和带参数的存储过程实例

    SQL中调用存储过程语句:call procedure_name(); 注:调用时”()”是不可少的,无论是有参数还是无参数. 定义对数据库存储过程的调用时1.无参数存储过程:{call proced ...

  3. ubuntu14.04修改mysql默认编码

    修改文件为/etc/mysql/my.cnf [client] default-character-set = utf8 (ps:client的设置没变) [mysqld] lower_case_ta ...

  4. 《Spark Python API 官方文档中文版》 之 pyspark.sql (三)

    摘要:在Spark开发中,由于需要用Python实现,发现API与Scala的略有不同,而Python API的中文资料相对很少.每次去查英文版API的说明相对比较慢,还是中文版比较容易get到所需, ...

  5. BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树

    同BZOJ3779. SDOI出原题,还是弱化版的. 吃枣药丸 #include <map> #include <cmath> #include <queue> # ...

  6. Eclipse + Apache Axis2 发布RESTful WebService(二)配置开发环境

    1. 下载axis2相关软件地址:http://axis.apache.org/axis2/java/core/download.html 2. 安装插件:将axis2-eclipse-codegen ...

  7. 【CF659E】New Reform(图的联通,环)

    分析转载自http://blog.csdn.net/yukizzz/article/details/51029628 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? ...

  8. “百度杯”CTF比赛 九月场_Test(海洋cms前台getshell)

    题目在i春秋ctf训练营 又是一道cms的通用漏洞的题,直接去百度查看通用漏洞 这里我使用的是以下这个漏洞: 海洋CMS V6.28代码执行0day 按照给出的payload,直接访问url+/sea ...

  9. PHP解码Json(json_decode)字符串返回NULL的原因及解决方法(转载)

    本文主要为大家讲解了php在使用json_decode函数解码json字符串时,解码不成功返回NULL的问题原因分析和解决方法,感兴趣的同学参考下. 一般来说,php对json字符串解码使用json_ ...

  10. C语言集锦(二) 图像显示 Windows和Linux

    关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库. 了解最原始的方式,对 ...