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

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

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

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

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

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

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

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

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

  9. [LeetCode] 139. Word Break 拆分词句

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

随机推荐

  1. 【STL】-迭代器的用法

    初始化: list<char>::iterator pos; 算法: 1. 遍历 for(pos = col1.begin(); pos != col1.end(); ++pos){... ...

  2. 基于百度定位及天气获取的DEMO

    demo基于百度定位APIv4.0版.新浪天气(不用查询城市代码). 需求: 1.button实现触发定位监听和天气捕获 2.两个textview 分别显示详细地址.天气. 界面很简陋,侧重功能实现. ...

  3. static关键字的理解

    #include<stdio.h> int counter(int i){ static int count=0;//编译时只运行一次 count=count+i; return coun ...

  4. Setup Factory 打包.netframework 2.0

    在setup factory 的安装目录下的Dependencies中新建目录dotnet20/并放入dotnetfx2.0.exe: Dependencies目录中再加xml文件dotnet20.x ...

  5. yeild

    正在使用cpu的线程,退出,返回等待池,其他优先级相同的线程竞争上岗.

  6. 转 Learning To Rank之LambdaMART的前世今生

    http://blog.csdn.net/huagong_adu/article/details/40710305

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

  8. WEB ui快速构建

    http://www.runoob.com/bootstrap/bootstrap-ui-editor.html 1http://pingendo.com/ 2http://www.layoutit. ...

  9. IOS @2X.png

    [UIImage imageNamed:@"xxx.png"] 或者xib里iPhone4会自动找*@2x.png initWithContentOfFile:pathToImag ...

  10. Emacs常用命令

    1.离开Emacs 挂起Emacs C-z 退出Emacs C-x C-c 2.文件 打开文件 C-x C-f 保存文件 C-x C-s 保存所有的文件 C-x s 将一个文件的内容插入到当前buff ...