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

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. cocos2dx JS 清除缓存重新编译打包安卓apk

    复制他人工程时打包出错,无法进行.或者是资源缓存问题需要重新编译删除 proj.android 工程下的三个文件夹 frameworks -> runtime-src -> proj.an ...

  2. (已解决)iOS真机运行 Xcode报错(libpng error: CgBI: unhandled critical chunk)

    Cocos2d-x加载图片资源出现libpng error: CgBI: unhandled critical chunk Xcode7.3 设置Remove Text Metadata From P ...

  3. RCNN系列算法的发展

    一. RCNN系列的发展 1.1  R-CNN 根据以往进行目标检测的方法,在深度学习应用于目标检测时,同样首先尝试使用滑动窗口的想法,先对图片进行选取2000个候选区域,分别对这些区域进行提取特征以 ...

  4. Sublime text3 经常出现 “ There are no packages avaliable for installation” 解决方法

    对应这个问题,一开始在度娘上找到很多答案,包括将json文件放在本地然后通过 package setting 更改的,发现其实不好使,原因未知. 后来测试了在hosts文件添加sublime text ...

  5. [ Learning ] Spring Resources

    1. Spring MVC Spring MVC原理及配置详解 springMVC系列之(三) spring+springMVC集成(annotation方式) Mybatis3+Spring4+Sp ...

  6. html5-嵌入图片

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

  7. python编程快速上手第7章习题20

    20.如何写一个正则表达式,匹配每 3 位就有一个逗号的数字?它必须匹配以下数字:'42''1,234''6,368,745'但不会匹配: '12,34,567' (逗号之间只有两位数字) '1234 ...

  8. MCMC算法深入理解

    MCMC(Markov Chain Monte Carlo),即马尔科夫链蒙特卡洛方法,是以马尔科夫平稳状态作为理论基础,蒙特卡洛方法作为手段的概率序列生成技术. MCMC理论基础 如果转移矩阵为P的 ...

  9. numpy高级应用

    reshape重塑数组 ravel 拉平数组 concatenate 最一般化的连接,沿一条轴连接一组数组 # print(np.concatenate([arr1,arr2],axis = 0)) ...

  10. 移动端点击返回时强制页面刷新解决办法(pageshow)

    在做移动端项目的时候经常遇到这样一个功能比如: 返回后页面不刷新,一些失效的信息依然显示在页面上.这个问题在iphone手机上会出现,在Android手机上返回时会自动刷新(由于手机机器种类不多,无法 ...