这是一题不太明显的动态规划,主要考察的应该是深度优先搜索。

static LinkedList<String> list = new LinkedList<String>();
static ArrayList<String> res=new ArrayList<String>();
public ArrayList<String> wordBreak(String s, List<String> set) {
list.clear();
res.clear();
if (s == null || s.length() == 0)
return res;
if (wordBreakcheck(s, set))
dfs(s, set);
return res;
} private void dfs(String s, List<String> set) {
if (s==null||s.length()==0) {
StringBuilder sb = new StringBuilder();
for (String ss : list) {
sb.append(ss);
sb.append(" ");
}
res.add(sb.toString().trim());
return;
}
for (int i = 1; i <= s.length(); i++) {
String str=s.substring(0, i);
if (set.contains(str)) {
list.add(str);
//s = s.substring(i, s.length());
dfs(s.substring(i, s.length()), set);
list.pollLast();
}
}
} public boolean wordBreakcheck(String s, List<String> set) {
if (s == null || s.length() == 0)
return true;
boolean[] res = new boolean[s.length() + 1];
res[0] = true;
for (int i = 0; i < s.length(); i++) {
StringBuilder str = new StringBuilder(s.substring(0, i + 1));
for (int j = 0; j <= i; j++) {
if (res[j] && set.contains(str.toString())) {
res[i + 1] = true;
break;
}
str.deleteCharAt(0);
}
}
return res[s.length()];
}

if (set.contains(str)) {
list.add(str);
dfs(s.substring(i, s.length()), set);
list.pollLast();
}

if (set.contains(str)) {
list.add(str);
s = s.substring(i, s.length());
dfs(s, set);
list.pollLast();
}

以上两种写法是完全不一样的,已经多次翻车。第一种写法,dfs函数执行完后s然后是没有切割的字符串,而第二种写法是dfs执行完后s已经是切割完的了。

动态规划之140 Word Break2的更多相关文章

  1. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  2. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  3. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

  4. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  5. Java for LeetCode 140 Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  6. leetcode 140. Word Break II ----- java

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  7. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

  8. leetcode@ [139/140] Word Break & Word Break II

    https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, determine ...

  9. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

随机推荐

  1. myeclipse修改了安装目录名字打不开解决方法

    在MyEclipse XX目录下有一个MyEclipse.ini的文件,里面既有相对路径,又有绝对路径,修改绝对路径指向新的位置即可 来源:http://www.iteye.com/problems/ ...

  2. Nuxtjs初始

    今天去看vue的官网,才看了他的升级版-->Nuxtjs,https://nuxtjs.org/guide/installation可以点击链接进入他的官网查看文档 第一步,搭建项目之前的准备工 ...

  3. php 静态方法 静态属性 和 普通方法 普通属性区别

    1,实例属性,是每个对象都可以不一样的数据,也是每个对象都“独自拥有”的数据: 2,静态属性,他不属于任何一个对象,而只属于该类本身,也可以理解为为所有对象所共有的数据:

  4. caffe中的caffemodel参数提取方法

    需要的文件为:deploy.prototxt caffemodel net = caffe.Net(deploy.txt,caffe_model,caffe.TEST)具体代码: import caf ...

  5. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...

  6. mysql批量插入,批量更新

    进行批量操作的时候,一定要事先判断数组非空 <insert id="batchInsert"parameterType="java.util.List"& ...

  7. ReactiveCocoa(III)

    flatMap(FlattenStrategy.latest) observe(on: UIScheduler()).startWithResult 切换线程: observeOn(UISchedul ...

  8. 【Hbase学习之三】Hbase Java API

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...

  9. MyBatis学习笔记(二)——使用MyBatis对表执行CRUD操作

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4262895.html 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用My ...

  10. python字典的排序,按key排序和按value排序---sorted()

    >>> d{'a': 5, 'c': 3, 'b': 4} >>> d.items()[('a', 5), ('c', 3), ('b', 4)] 字典的元素是成键 ...