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

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. EasyTouch中虚拟摇杆的使用EasyJoystick

    unity3d自带的虚拟摇杆显然没有EasyTouch好用 首先下载这个插件 http://pan.baidu.com/s/1hqJAbTa 下载完成后.导入到unity,可以看看里面的案例

  2. python知识补足

    1.class的init功能,初始化class,给出一些初始值 __init__可以理解成初始化class的变量,取自英文中initial 最初的意思.可以在运行时,给初始值附值, class Cal ...

  3. Linux SSH 免秘钥登录

    SSH 免秘钥登录 ssh:是一种安全加密协议 ssh  username@hostname     ssh gongziyuan.com:以当前用户登录该机器(如果不是当前用户,需要这么干:ssh ...

  4. Python记录8:函数的嵌套

    #函数的嵌套分为两类:# 1.函数的嵌套定义: 在函数内部又定义了一个函数# def foo():# x=1# # print(x)# def bar():# print('from bar')## ...

  5. 3.GDScript(1)概览

    GDScript 是上面提到的用于Godot的主要语言.和其他语言相比,它与Godot高度整合,有许多优点: 简单,优雅,设计上为Lua.Python.Squirrel等语言用户所熟悉. 加载和编译速 ...

  6. linux基础操作

    1.pwd 2.clear 3.who 4.cal 5.uname 6.wc 7.man在线帮助命令 8.--help.info.whatis 9.使用su命令以root身份进入linux 10.Sh ...

  7. Rower Bo (高数 + 物理)

    #include<bits/stdc++.h> #define esp (1e-5) using namespace std; int main(){ int a; double v1, ...

  8. C语言学习感受

    C语言,是我学习的第一种计算机语言,是他作为我编写程序的开始,在学习的时候,先学习了最基础的知识,在语言的理论学习语法上,我逐渐的了解了C语言并且对他有了基础的认识与理解,随着学习内容的不断深入,我逐 ...

  9. 集合运算—union(并集)、intersect(交集)和except(差集)

    一.集合运算的基本格式是: 集合查询1 <集合运算> 集合查询2 [order by ...] 二.集合运算符是对两个集合操作的,两个集合必须具有相同的列数,列具有相同的数据类型(至少能隐 ...

  10. 区块链公链分片技术(sharding)方案,配思维导图

    区块链公链分片技术(sharding)方案,配思维导图 分片技术(sharding)方案 以太坊分片思路 其基本思想是,将网络中的节点分成不同的碎片,各分片可以并行处理不同交易,这样可以并行处理相互之 ...