Java for LeetCode 139 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"
.
解题思路一:
直接暴力枚举会导致TLE,因此,需要记录之前的结果,即可以采用dp的思路,JAVA实现如下:
- static public boolean wordBreak(String s, Set<String> wordDict) {
- boolean[] dp = new boolean[s.length() + 1];
- dp[0] = true;
- for (int i = 1; i < dp.length; i++)
- for (int j = i; j >= 0 && !dp[i]; j--)
- if (wordDict.contains(s.substring(i - j, i)))
- dp[i] = dp[i - j];
- return dp[dp.length - 1];
- }
解题思路二:
考虑到下题用dp做不出来,暴力枚举肯定TLE,所以可以设置一个unmatch集合来存储s中已经确定无法匹配的子串,从而避免重复检查,JAVA实现如下:
- static public boolean wordBreak(String s, Set<String> dict) {
- return wordBreak(s, dict, new HashSet<String>());
- }
- static public boolean wordBreak(String s, Set<String> dict,
- Set<String> unmatch) {
- for (String prefix : dict) {
- if (s.equals(prefix))
- return true;
- else if (s.startsWith(prefix)) {
- String suffix = s.substring(prefix.length());
- if (!unmatch.contains(suffix)) {
- if (wordBreak(suffix, dict, unmatch))
- return true;
- else
- unmatch.add(suffix);
- }
- }
- }
- return false;
- }
Java for LeetCode 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 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- Java for LeetCode 140 Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode #139. Word Break C#
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [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(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
随机推荐
- 【BZOJ-3172】单词 AC自动机
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2567 Solved: 1200[Submit][Status ...
- SpringMVC配置
博客园 闪存 首页 新随笔 联系 管理 订阅 随笔- 4 文章- 1 评论- 0 搭建springmvc框架的另一种思路 在一个完整的项目里搭建springmvc框架的时候, 通常情况下,初学者 ...
- bzoj1069 SCOI2007 最大土地面积
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2560 Solved: 983 Description ...
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- MVC执行顺序
MVC在底层和传统的asp.net是一致的,在底层之上,相关流程如下: 1)Global.asax里,MvcApplication对象的Application_Start()事件中,调用 RouteC ...
- KxMenu下拉菜单
+ (void)createMenu:(id)sender target:(UIViewController *)t { NSArray *menuItems = @[ [KxMenuItem men ...
- Unity教程之再谈Unity中的优化技术
这是从 Unity教程之再谈Unity中的优化技术 这篇文章里提取出来的一部分,这篇文章让我学到了挺多可能我应该知道却还没知道的知识,写的挺好的 优化几何体 这一步主要是为了针对性能瓶颈中的”顶点 ...
- centos linux从无到有安装wordpress
序:本博客从无到有搭建wordpress,包括从服务器和域名购买,会将步骤一步一步记录下来.如果你也是新手,那你有福了,因为我的系统是centos,对号入座啊. 目录 一.准备域名和服务器一.安装ph ...
- linux命令合集
序:介绍一些经常需要用到的Linux命令. 一.wget 作用:下载网络文件,将远程服务器文件恢复备份到本地. wget http://cn.wordpress.org/wordpress-3.1-z ...
- 读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1
<白帽子讲Web安全>吴翰清著 刚开始看这本书就被这本书吸引,感觉挺不错,给大家推荐下,最近读这本书,感觉不错的精华就记录下, 俗话说>>>好脑袋不如一个烂笔头< ...