【LeetCode】Word Break 解题报告
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 解题报告的更多相关文章
- 【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 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 ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- [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]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [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 473.火柴拼正方形
火柴拼正方形 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为 ...
- Android library projects cannot be launched解决方法
着了一个例子项目,总是报标题说的错误. 解决方法如下: 红圈的地方,勾掉. 貌似如果你这个项目是作为一个被引用的project的话, 要勾上这个.单独作为一个app的话,不能勾选这个. --不懂,瞎写 ...
- 近期JS心得
child和tags都是[{id:1,value:'a'}]的格式,当点击一级标签,要看二级标签是否已经被选中,如果被选中,则清除出去 如果用for循环 再splice的话 当删除掉了一个元素后,数组 ...
- Welcome-to-Swift-09类和结构体(Classes and Structures)
类和结构体是人们构建代码所用的一种通用且灵活的构造体.为了在类和结构体中实现各种功能,我们必须要严格按照对于常量,变量以及函数所规定的语法规则来定义属性和添加方法. 与其他编程语言所不同的是,Swif ...
- 周赛Problem 1021: 分蛋糕(埃拉托斯特尼筛法)
Problem 1021: 分蛋糕 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %lld ...
- Java连接SQLite数据库
下载java包:sqlite-jdbc-3.7.2.jar,放到java工程目录lib下 如下代码实例: import java.sql.*; import org.sqlite.JDBC; /** ...
- spring-boot项目热部署以及spring-devtools导致同类不能转换
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- 单线程实现并发——协程,gevent模块
一 并发的本质 1 切换 2 保存状态 二 协程的概念 协程,又称微线程,纤程.英文名Coroutine.单线程下实现并发,用户从应用程序级别控制单线程下任务的切换,注意一定是遇到I/O才切. 协程的 ...
- Bzoj2038 小Z的袜子(hose)
Time Limit: 20000MS Memory Limit: 265216KB 64bit IO Format: %lld & %llu Description 作为一个生活散漫 ...
- javascript事件捕获机制,dom tree
$(document,"a").on("click",function(){alert(2);return false;}); $("<a> ...