【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 ...
随机推荐
- Numpy ndarray 的高级索引存在 "bug" ?
Numpy ndarray 高级索引 "bug" ? 话说一天,搞事情,代码如下 import numpy as np tmp = [1, 2, 3, 4] * 2 a, b = ...
- phpcms 后台也名称
announce 公告 show.html 内容页 comment 评论 show_list.html 内容页评论列表 list.html 评论列表 content 内容模型 category.htm ...
- javascript学习笔记 - 引用类型 Array
二 Array 1.可以通过length属性删除或创建新的数组项 arr = [1,2,3]; arr.length = 4;//增加 [1,2,3,undefined] arr.length = 2 ...
- iOS学习笔记41-Swift(一)基础部分
一.Swift语言介绍 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题. Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种 ...
- C++之Effective STL学习笔记Item7
假设我们现在有以下代码: void doSomething() { vector<Widget*> vwp; ; i < SOME_MAGIC_NUMBER; ++i) vwp.pu ...
- [BZOJ3261] 最大异或和 (异或前缀和,可持久化Trie)
Description 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x:询问操作, ...
- jenkins使用xvfb插件构建虚拟化显示屏自动化测试
1.linux服务器安装xvfb,并启动 参考我的博客:http://www.cnblogs.com/lincj/p/5468505.html 或者网上搜索一下进行安装 2.jenkins安装xvfb ...
- 用Keepalived搭建双Nginx server集群,防止单点故障
综述: 浏览器访问虚拟IP: 192.168.1.57, 该虚拟IP被Keepalived接管,两个Keepalived进程分别运行在物理IP为192.168.1.56和192.168.1.59服务器 ...
- 转 python基础学习笔记(一)
http://www.cnblogs.com/fnng/category/454439.html 下面我们创建一个文件 root@fnngj-H24X:/hzh/python# touch hell. ...
- Django ConnectionAbortedError WinError 10053 错误
因为ajax默认是异步提交,可是有时候我们会发现,本来要求请求马上出现,可是异步会导致后面突然再执行,这样就出问题了. (1)添加这样一段代码 $.ajaxSetup({ async : false ...