【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
【139-Word Break(单词拆分)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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,确定s能否够切割成一个或多个单词空格分隔的序列。
解题思路
一个字符串S,它的长度为N。假设S能够被“字典集合”(dict)中的单词拼接而成,那么所要满足的条件为:
* F(0, N) = F(0, i) && F(i, j) && F(j, N);
* 这样子,假设我们想知道某个子串是否可由Dict中的几个单词拼接而成就能够用这种方式得到结果(满足条件为True, 不满足条件为False)存入到一个boolean数组的相应位置上,这样子,最后boolean 数组的最后一位就是F(0, N)的值,为True表示这个字符串S可由Dict中的单词拼接,否则不行!
代码实现
算法实现类
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
// 參数校验
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
// 标记是否匹配,match[i]表表[0, i-1]都匹配
int length = s.length();
boolean[] match = new boolean[length + 1];
match[0] = true;
for (int i = 1; i < length + 1; i++) {
for (int j = 0; j < i; j++) {
if (match[j] && wordDict.contains(s.substring(j, i))) {
// f(0,n) = f(0,i) + f(i,j) + f(j,n)
match[i] = true;
break;
}
}
}
return match[length];
}
// 以下是还有一种解法。可是会超时
public boolean wordBreak2(String s, Set<String> wordDict) {
// 參数校验
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
Map<Character, Set<String>> wordMap = new HashMap<>(wordDict.size());
// 将全部開始字符同样的单词放入一个Set中
for (String word : wordDict) {
Set<String> set = wordMap.get(word.charAt(0));
if (set == null) {
// 新创建一个set放入Map中
set = new HashSet<>();
wordMap.put(word.charAt(0), set);
}
// 单词存入set中
set.add(word);
}
return wordBreak(s, 0, wordMap);
}
/**
* 搜索字符串能否够被切割成单词串
*
* @param s 字符串
* @param idx 处理的開始位置
* @param wordMap 单词字典,開始字符同样的在同一个set集合中
* @return 搜索结果
*/
public boolean wordBreak(String s, int idx, Map<Character, Set<String>> wordMap) {
if (idx >= s.length()) {
return true;
}
Set<String> words = wordMap.get(s.charAt(idx));
if (words != null) {
for (String word : words) {
// idx之前的字符已经匹配,假设从ide之后起匹配word单词
if (s.startsWith(word, idx)) {
// 递归处理
boolean result = wordBreak(s, idx + word.length(), wordMap);
// 假设满足条件,返回true
if (result) {
return true;
}
}
}
}
return false;
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47774547】
【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 139 Word Break 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Leetcode139. Word Break单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...
- 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- 【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】
[079-Word Search(单词搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a 2D board and a word, find if ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
随机推荐
- ZigBee学习一 任务处理函数_ProcessEvent
ZigBee学习一 任务处理函数_ProcessEvent //任务处理函数UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events ){ ...
- 如何在 Windows 7 上安装 TeX Live 2018
$\color{red}{\mathsf{UPDATE}}$:见李阿玲在知乎专栏 All about TeXnique 发布的安装教程 关于 TeX Live:http://tug.org/texli ...
- BZOJ3772 精神污染 【主席树 + dfs序】
题目 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户,海 ...
- 5-Dalvik垃圾收集机制Cocurrent GC
Dalivik垃圾回收收机制Cocurrent GC简介和学习计划 导语: 在C/C++中,开发者需要手动地管理在堆中分配的内存,但是这往往导致很多问题. 1. 内存分配之后忘记释放,造成内存泄漏. ...
- APK包与类更改分析
360APK包与类更改分析 1 题目要求 这是360的全球招募无线攻防中的第二题,题目要求如下: 1)请以重打包的形式将qihootest2.apk的程序包名改为 "com.qihoo.cr ...
- Unity使用 16bit 压缩 Texture 颜色能均匀过渡
下面是unity自带 16bit 图 的效果,可以看到颜色过度的很不均匀,占用内存 0.5M 如果调成 truecolor 后 颜色过渡很均匀,而内存却占到 1.1 M 讲图片 后缀名改成 ...
- BZOJ 3640: JC的小苹果
3640: JC的小苹果 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 425 Solved: 155[Submit][Status][Discus ...
- RazorExtensions Templated Razor Delegates
原文发布时间为:2011-04-27 -- 来源于本人的百度文章 [由搬家工具导入] Templated Razor Delegates David Fowler turned me on to a ...
- AJAX在VS2005中的简单应用 使用ajaxpro.2.dll[点击按钮执行事件不刷新]
原文发布时间为:2008-10-21 -- 来源于本人的百度文章 [由搬家工具导入] 1.下載ajaxpro.dll或AjaxPro.2.dll 放在Bin文件夹中2.配置web.config 3.u ...
- [LeetCode] Binary Search Tree Iterator 深度搜索
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...