leetcode 139. Word Break ----- 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".
查看字符串是否由字典中的单词组成。
1、暴力递归,果断超时。
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
return helper(s,0,wordDict);
}
public boolean helper(String s,int start,Set<String> wordDict ){
if( start == s.length() )
return true;
for( int i = s.length();i > start ;i--){
if( wordDict.contains( s.substring(start,i) ) ){
if( helper(s,i,wordDict) )
return true;
}
}
return false;
}
}
2、dp,基本达到最快。
int len = s.length(),maxLen = 0;
boolean[] dp = new boolean[len];
for( String str : wordDict ){
maxLen = Math.max(maxLen,str.length());
} for( int i = 0 ;i<len;i++){ for( int j = i;j>=0 && i-j<maxLen;j-- ){
if( ( j == 0 || dp[j-1] == true ) && wordDict.contains(s.substring(j,i+1)) ){
dp[i] = true;
break;
}
}
} return dp[len-1];
leetcode 139. Word Break ----- java的更多相关文章
- [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 ...
- 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-separa ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 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 ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- 【STL】-迭代器的用法
初始化: list<char>::iterator pos; 算法: 1. 遍历 for(pos = col1.begin(); pos != col1.end(); ++pos){... ...
- 基于百度定位及天气获取的DEMO
demo基于百度定位APIv4.0版.新浪天气(不用查询城市代码). 需求: 1.button实现触发定位监听和天气捕获 2.两个textview 分别显示详细地址.天气. 界面很简陋,侧重功能实现. ...
- static关键字的理解
#include<stdio.h> int counter(int i){ static int count=0;//编译时只运行一次 count=count+i; return coun ...
- Setup Factory 打包.netframework 2.0
在setup factory 的安装目录下的Dependencies中新建目录dotnet20/并放入dotnetfx2.0.exe: Dependencies目录中再加xml文件dotnet20.x ...
- yeild
正在使用cpu的线程,退出,返回等待池,其他优先级相同的线程竞争上岗.
- 转 Learning To Rank之LambdaMART的前世今生
http://blog.csdn.net/huagong_adu/article/details/40710305
- 2016 - 1- 22 Build a Nav bar (intro to HTML&CSS)
一:Learn how to build a NavBar --- allow user navigate ur site 1. The hypetext refrence link This att ...
- WEB ui快速构建
http://www.runoob.com/bootstrap/bootstrap-ui-editor.html 1http://pingendo.com/ 2http://www.layoutit. ...
- IOS @2X.png
[UIImage imageNamed:@"xxx.png"] 或者xib里iPhone4会自动找*@2x.png initWithContentOfFile:pathToImag ...
- Emacs常用命令
1.离开Emacs 挂起Emacs C-z 退出Emacs C-x C-c 2.文件 打开文件 C-x C-f 保存文件 C-x C-s 保存所有的文件 C-x s 将一个文件的内容插入到当前buff ...