【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 ...
随机推荐
- adb pull 文件夹的时候注意
传说之美 分享快乐 记录生活 学习探索 博客园 首页 新随笔 联系 管理 订阅 随笔- 75 文章- 0 评论- 19 Android 用adb pull或push 拷贝手机文件到到电脑上,拷贝 ...
- python ATM大作业之alex思路
一 ATM alex想了一个思路,就是定义一个函数,这个函数可以实现所有的atm的功能:取款,转账,消费等等. 为了实现这个想法,alex构建了一个两级字典,厉害了.我发现,厉害的人都喜欢用字典.这里 ...
- 外星人(bzoj 2749)
Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Output 3 HINT Test< ...
- node(总结)--整体
- vue.js源码学习分享(七)
var _Set; /* istanbul ignore if */ if (typeof Set !== 'undefined' && isNative(Set)) { // use ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- du 查看 資料夾 佔用空間
查看 目前目錄使用的空間大小 du -h --max-depth=0 -h, --human-readable 查看 目前及下一屠的目錄 使用的空間大小 du -h --max-depth=1
- 获取当前网络中的电脑数目及MAC-通过MAC查找IP-通过IP查询机器名
Microsoft Windows [版本 ] 版权所有 (c) Microsoft Corporation.保留所有权利. C:\Users\Administrator>netsh netsh ...
- SQLite的sqlite_sequence表
SQLite的sqlite_sequence表 sqlite_sequence表也是SQLite的系统表.该表用来保存其他表的RowID的最大值.数据库被创建时,sqlite_sequence表会 ...
- CodeForces - 600F Edge coloring of bipartite graph
Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...