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

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. list异常

    可能定义的时候没有初始化把 private List<Msg> msgList = new ArrayList<>(); 为啥我android老是忘了new ArrayList

  2. 当我的url请求会变成jsp页面路径时的解决办法

    @RequestMapping(value="shippingOrder") $.post("/ezsh/orderAd/shippingOrder",para ...

  3. C# 对数据库操作的帮助类SQLHelper.cs

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  4. hbase-java-api001

    package api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfig ...

  5. linux系统状态检测命令

    1.ifconfig命令 ifconfig命令用于获取网卡配置与网络状态等信息,格式为“ifconfig [网络设备] [参数]”. 使用ifconfig命令来查看本机当前的网卡配置与网络状态等信息时 ...

  6. python 文件写入错误

    在保存网页文字到txt文件下时,出现如下错误 UnicodeEncodeError: 'gbk' codec can't encode character u'\xa9' in position 24 ...

  7. react修改app.js添加中文内容后中文部分乱码解决

    [问题]:配置完react后修改app.js内容时添加中文出现如下乱码的中文. [A解决]文档——文本编码——转换文本编码,在弹出窗口修改,确定,搞定 [B解决]首先在EditPlus内:工具——首选 ...

  8. linux常用命令:cp 命令

    cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果是 ...

  9. Runtime单例模式类 -- 控制电脑关机

    package demo1; import java.io.IOException; public class RunTimeDemo { public static void main(String ...

  10. linux mysql主从复制

    centos7 安装 mariadb 1 yum 源  -- 配置阿里的 2 rmp 方式 3 源码编译方式  -- 专业DBA 一些知识点: 虚拟环境 不影响 redis/ mariadb/mysq ...