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

分析如下:

题目意思是,给定词典的情况下,看看原字符串能不能全部成功地被给定的词典分割。也就是说,无论对原字符串怎么切割,只要切割后的那些字符串都在dict中即可。比如:"leetcode"可以切割成"le","et","code",只要dict包含这三个字符串就行。例子中当对字符串切割成"leet","code"时,刚好这两个字符串都在dict中,返回true。

一开始,最自然的想法是,使用递归。如果s本身就在dict中,返回true,否则就从前往后遍历字符串,取前面子串,当前面部分在dict中,就递归判断剩下的字符串。

见代码:但是递归会超时

class Solution {
public boolean wordBreak(String s, List<String> wordDict) { return helper(s,wordDict,0);
}
public boolean helper(String s,List<String> word,int index){
if(word.contains(s.substring(index))) return true; for(int i=index;i<s.length()-1;i++){
if(word.contains(s.substring(index,i+1))&&helper(s,word,i+1)){
return true;
}
}
return false;
}
}

使用动态规划,用一个数组存放从开头第一个字符到第i个字符(前面长度为i的字符串)能否切分成功flag[i]。i从1开始。
判断第i个字符这里的flag[i],只要找到第i个字符前面的位置j上,flag[j]为true,而且从第j+1个字符到i个字符的字符串在dict中,则表示从第一个元素到第i个元素的字符串符合要求,flag[i]=true;

比如计算leetcode的分割方式,假设计算从开头长度为5的字符串(leetc)是否符合要求,也就是求flag[5]。只要前面有一个满足:flag[j]=true(j<5),而且从第j+1个元素到第5各元素的字符串在dict中,就行。

class Solution {
public boolean wordBreak(String s, List<String> wordDict) { /*
使用动态规划,用一个数组存放从开头第一个字符到第i个字符(前面长度为i的字符串)能否切分成功flag[i]。i从1开始。
判断第i个字符这里的flag[i],只要找到第i个字符前面的位置j上,flag[j]为true,而且从第j+1个字符到i个字符的字符串在dict中,则表示从第一个元素到第i个元素的字符串符合要求,flag[i]=true;
*/
if(wordDict.contains(s)) return true;
//数组表示从开头到第i个元素(前面长度为i的字符串)能否切分成功flag[i]。i从1开始
boolean[] flag=new boolean[s.length()+1];
flag[0]=true; //0用于判断第一个元素 for(int i=1;i<=s.length();i++){ //这里i表示字符串的第i个元素,这里没有从0开始
/*判断第i个元素前面各个长度的字符串的情况,当长度j的字符串(从第一个元素开始到第j个元素)能够切分,那么就判断从第j+1个元素到第i个元素这个字符串在不在dict中。注意使用substring时下标是从0开始,要注意
*/
for(int j=0;j<i;j++){ if(flag[j]&&wordDict.contains(s.substring(j,i))) {
flag[i]=true;//i=0时,是字符串的第一个字符,所以在falg中下标为1。
break;
}
} } return flag[s.length()]; } }

Word Break(动态规划)的更多相关文章

  1. LeetCode -- Word Break 动态规划,详细理解

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  2. 139. Word Break(动态规划)

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

  3. word break II(单词切分)

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

  4. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

  5. LeetCode ||& Word Break && Word Break II(转)——动态规划

    一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...

  6. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  7. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  8. Leetcode#139 Word Break

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

  9. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

随机推荐

  1. 最简单的基于FFmpeg的libswscale的示例附件:测试图片生成工具

    ===================================================== 最简单的基于FFmpeg的libswscale的示例系列文章列表: 最简单的基于FFmpeg ...

  2. DAA和CMAC

    数据认证算法(DAA)  Data Authentication Algorithm DAA建立在DES之上,该算法比较陈旧,人们已经发现了这个算法的安全弱点,目前已经被废止. DAA采用DES运算的 ...

  3. Ubuntu 15.10下Qt5的安装实战

    写照篇博客的目的就是因为最近要使用Qt,但是由于本人的系统是Ubuntu的,而网上大部分的讲解全是基于Windows的,所以就花费一些时间总结了一下我的安装过程,当然也是也为了能帮助到更多的博友. 第 ...

  4. Zeroc Ice原理介绍

    Ice介绍         Ice(Internet Communications Engine)是ZeroC公司的杰作,继承了CORBA的血统,是新一代的面向对象的分布式系统中间件.Ice是RPC通 ...

  5. ROS_Kinetic_16 ubuntu中安装使用Matlab和ROS

    ROS_Kinetic_16 ubuntu(16.04)中安装使用Matlab(2015b)和ROS(kinetic) 参考网址:http://cn.mathworks.com/hardware-su ...

  6. Java中数组的扩容

    在写程序的过程中,我们常常会碰见数组空间不够用的情况,比如我已经初始化了一个数组int []a = {1,2,3,4,5,6,7,8,9,10} ;这时,我想往数组下标3的位置插入一个元素,该怎么做? ...

  7. Java 8新特性探究(二)类型注解和重复注解

    本文将介绍java 8的第二个特性:类型注解. 注解大家都知道,从java5开始加入这一特性,发展到现在已然是遍地开花,在很多框架中得到了广泛的使用,用来简化程序中的配置.那充满争议的类型注解究竟是什 ...

  8. Unity StrangeIoC HelloWorld

    unity有一个叫StrangeIoC的框架插件,这里写了一个使用StrangeIoC的HelloWorld,比他自带的demo更为简单,方便理解 1.插件下载,在Asset Store直接搜索Str ...

  9. 【Android 应用开发】Activity 状态保存 OnSaveInstanceState参数解析

    作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38297083 一. 相关方法简介 1. 状态保存方法示例 p ...

  10. memcached 详解

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...