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的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  3. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  4. 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 ...

  5. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  6. 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 ...

  7. 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 ...

  8. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  9. [LeetCode] 139 Word Break(BFS统计层数的方法)

    原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...

随机推荐

  1. 在VS2012中编译WinXP兼容的程序

    VS2012默认是不兼容Windows XP的,编译链接出来的程序只能在Windows Vista及以上版本的操作系统上运行.可是有时需要在Windows XP上运行,又不得不用VS2012(例如用了 ...

  2. poj 3311 tsp入门

    题意:n+1个点:0--n,找一条路径从0点出发遍历1--n的点再回到0,每个点可经过不止一次,求最短路径 裸的TSP问题,先用Floyd求出各个点之间最短路,再状压dp即可 用n+1位二进制表示状态 ...

  3. easyui datagrid 通过复选框删除新追加的数据问题

    之前写好的功能在保存好数据后再通过复选框删除是没有问题的,可现在想多追加几行,然后选择删除新追加的某几行或一行,通过$('#dg').datagrid('getChecked')方法返回选中行,然而返 ...

  4. Linux下不同机器之间的文件拷贝

    通过 scp 命令实现不同机器之间的文件拷贝. (1)本机考到目标机器:scp 本机文件 目的地: 如:scp /home/odp-web.war   root@192.168.6.137:/usr/ ...

  5. The mean shift clustering algorithm

    The mean shift clustering algorithm MEAN SHIFT CLUSTERING Mean shift clustering is a general non-par ...

  6. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  7. centos 7 的几点改动

    1.运行级别 旧:/etc/inittab 新:/etc/ststemd/system  例:ln -sf /lib/systemd/system/multi-user.target /etc/sys ...

  8. mysql 视图(view)

    什么是视图 视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据. 这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据. 数 ...

  9. 基于.NET的大型Web站点StackOverflow架构分析(转)

    Stack Overflow网址:http://stackoverflow.com/ 当前访问量:每月9500PV(每天300多万PV) 当前Alexa排名:149 所用.NET技术:C#.Visua ...

  10. Substance风格实例大全javaswing皮肤风格大全(原)

    转载请注明来路:http://www.cnblogs.com/langtianya/ 下图是所支持的设置 下面把上面的每一个选项的子选项进行试验 substance皮肤: AutumnSkin Bus ...