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. [转载]android的消息处理机制(图+源码分析)——Looper,Handler,Message

    2013-12-18 14:17:33 转载自: http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html 请跳转到转载 ...

  2. MySql 性能优化杂记

    前一段时间接触MySql 服务器,关于查询忧化方面整理,优化主要唯绕几个工具函数 : show profiling  , explain ,  索引 , limit 如果上司抱怨服务器查询太慢,这时候 ...

  3. CodeForces 468A Program F

    Description Little X used to play a card game called "24 Game", but recently he has found ...

  4. 转:115个Java面试题和答案——终极列表(上)

    转自:http://www.importnew.com/10980.html 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的 ...

  5. jQuery 中 children() 与 find() 用法的区别

    1.children() 与 find() 用法的区别 通过children获取的是该元素的下级元素,而通过find获取的是该元素的下级所有元素.

  6. How to Avoid OOM in Android

    1.use java reference(strong soft weak phantom) 2.use android:largeHeap="true" above or VMR ...

  7. 一维条形码攻击技术(Badbarcode)

    0x00 前言 在日常生活中,条形码随处可见,特别在超市,便利店,物流业,但你们扫的条形码真的安全吗?之前TK教主 在PacSec介绍的条形码攻击和twitter上的demo视频太炫酷,所以就自己买了 ...

  8. vim1

    Vim模式介绍 几乎所有的编辑器都会有插入和执行命令两种模式,并且大多数的编辑器使用了与Vim接入不同的方式:命令目录(鼠标或者键盘驱动),组合键(CTRL和ALT组成)或鼠标输入.Vim和vi一样, ...

  9. jQuery Transit

    http://code.ciaoca.com/jquery/transit/ jQuery Transit 事件监听 https://developer.mozilla.org/en-US/docs/ ...

  10. 常用的php字符串处理函数

    php常用的字符串处理函数 1.trim():从字符串的两端删除空白字符和其他预定义字符 ltrim():从字符串的左端删除空格和其他预定义字符 rtrim():从字符串的末端开始删除空白字符和其他预 ...