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. Aandroid TV 基于Leanback支持最新MD设计的TV开发框架

    原文地址:http://blog.csdn.net/sk719887916 作者:skay 基于6.0最新的API 支持TV的框架 Android 6.0已完美支持TV开发,之前的5.0后Recycl ...

  2. WebLogic11g-负载分发

    负载均衡的实现方式有很多种,这里只介绍三种相对来说成本较低的方案(维护成本以及费用成本)weblogic自带的proxy.apache.nginx  1.weblogic自带的proxy方式:  1) ...

  3. 【翻译】Ext JS 6有什么新东西?

    工具包ToolKits 发布 包的命名 Fashion 图表 ItemEdit插件 网格 电子表格 可操作模式Actionable Mode和可访问性 LazyItems插件 屏幕阅读器支持可访问性 ...

  4. (一)php的基本知识和一些注意点

    注意:任何程序,包括php,在运行时都在内存中进行,php代码需要被读取到内存中才能执行. [php的运行方式] 1.通过服务器(例如apache)调用. 2.通过命令行调用(不需要服务器参与,因为没 ...

  5. 在mysql数据库中创建oracle scott用户的四个表及插入初始化数据

    在mysql数据库中创建oracle scott用户的四个表及插入初始化数据 /* 功能:创建 scott 数据库中的 dept 表 */ create table dept( deptno int ...

  6. ConcurrentHashMap和HashTable的区别

    hashtable是做了同步的,hashmap未考虑同步.所以hashmap在单线程情况下效率较高.hashtable在的多线程情况下,同步操作能保证程序执行的正确性. 但是hashtable每次同步 ...

  7. (一)UI设计的一些常识

    一.概述 新版本的Xcode似乎框架不明示. UIView:屏幕上看得见摸得着的东西.视图.控件.组件. UIView是一个容器,能容纳其他UIView. UIViewController用来控制UI ...

  8. 更新Cocos2D支持Xcode 7

    原文链接(有节选简写) Apple已经释放出Xcode7,给我们带来了Swift2和每个平台的最新版本支持.Cocos2D却还在等待更新去兼容Apple的改变.不幸的是,SpriteBuilder还未 ...

  9. Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用

     1 修改pom.xml,添加依赖文件: <dependency> <groupId>com.whalin</groupId> <artifactId&g ...

  10. Understanding Android Security(安卓安全的理解)

    论文作者: Enck, William Ongtang, MacHigar McDaniel, Patrick 下一代的开放操作系统不会在个人主机和大型主机上出现,而是在只能手机上.新环境的开放性将会 ...