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. tabbarcontroller 内嵌导航 控制器,2层push hide tabbar 后 ,第二层直接返回根视图控制器选择tabbarcontroller的其它vc 无法显示 tabbar的 问题解决方案

    场景如标题 这样不行: [self.navigationController popToRootViewControllerAnimated:YES]; MainViewController *mai ...

  2. Hibernate 的配置文件

    Hibernate 配置文件 •Hibernate 配置文件主要用于配置数据库连接和 Hibernate 运行时所需的各种属性 •每个 Hibernate 配置文件对应一个 Configuration ...

  3. Android打开新的Activity并同时关闭当前Activity

    Intent it = new Intent(); it.setClass(EditActivity.this, MainActivity.class); it.setFlags(Intent.FLA ...

  4. 详解x86、IA-32、IA-64等CPU系列

    x86架构起源于Intel公司在1978年推出的8086处理器.8086在1981年为IBM PC所选用,之后x86便成为了个人电脑的标准平台,成为历上最成功的CPU架构.8086是一款16位CPU, ...

  5. Maven 建立web项目 The import javax.servlet cannot be resolved

    右击项目,选择Java Build Path->Libraries->Add External JARs,tomcat的路径lib文件夹下选中"servlet-api.jar&q ...

  6. MyQQ 前言

    从接触IOS以来也将近8个月了,学习了不少知识. 看了一系列的书籍如: <objective-c 开发入门>--语法介绍,学习一门语言是开发的基础.所以这是我大致对语法语句学习的锻炼地方, ...

  7. JavaScript 时间特效 显示当前时间

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. 乌龟棋(noip2010)

    分析:该题是经典的动态规划题目. 题目中涉及到卡片数.卡片分4类.格子数等若干信息,又每张卡片仅能使用一次.求到达终点最多能能获得多少分. 从题目中可知卡片的使用顺序影响最终得分,我们可知状态转移和使 ...

  9. 《day12---异常》

    //91-面向对象-异常-异常的发生和简单应用. /* 异常: java运行时期发生的问题就是异常. Java中运行时的除了异常Exception含有错误Error. 异常:通常发生后可以有针对性的处 ...

  10. Sublime Text2 jedi插件离线安装

    1.Sublime Text2 下载安装 2.下载jedi    gitbub上的,https://github.com/srusskih/SublimeJEDI 3.打开sublime后,组合键“c ...