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

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. System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    2017/8/15 20:55:21 [AgentPayQuery_205506102_1BBBB]系统异常:System.Data.Entity.Core.EntityException: The ...

  2. VirtualBox下扩容vdi文件

    VirtualBox下扩容vdi文件 版本:VirtualBox 5.0.14 之前VirtualBox创建的虚拟机的vdi文件过小,无法满足新的实验需求,扩容vdi文件的方法如下: 比如我这里将RH ...

  3. Nuxtjs初始

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

  4. hbase-java-api003(put list)

    package api; import java.io.IOException; import java.util.ArrayList; import java.util.List; import o ...

  5. TP图片上传

    //控制器文件 public function index(){ if(!empty($_POST)){ $file = $_FILES["file"]; if(!isset($f ...

  6. 使用js调用js

    直接上源码: <div class="choose"> choose a mode<br> <hr> <button type=" ...

  7. plsql注册-转

    注册码:Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca https://blo ...

  8. 【swiper轮播插件】解决swiper轮播插件触控屏问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Qt读取TXT文件时,GBK与UTF-8编码判断

    读取txt文件时,很多时候无法获取文件的编码格式.如果直接进行使用,则有可能出现乱码.需要在使用前将其转为Unicode(Qt的默认编码格式). 虽然实际的编码格式种类非常多,但平常主要使用的有GBK ...

  10. input 的radio checkbox 和 select 相关操作

    1  select 获取和设置值,以及onchange事件 1下拉框option没有checked事件 可通过select 的 onchange事件进行监控,以获取其值 <select name ...