1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc139.java 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-07-15 14:01 . 20190715
package code;
/*
* 139. Word Break
* 题意:是否能够分词
* 难度:Medium
* 分类:Dynamic Programming
* 思路:动态规划
* Tips:巧妙的方法,防止了复杂的操作,通过遍历之前计算出来的结果
* 递归的方法本质和dp是一样的,记住用备忘录算法,把之前的结果记下来
* lc140
*/
import java.util.HashMap;
import java.util.List;
public class lc139 {
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] dp = new boolean[s.length()+1];
dp[0] = true;
for (int i = 1; i < dp.length ; i++) {
for (int j = 0; j < i ; j++) { //遍历之前计算出来的结果
if( dp[j]==true && wordDict.contains(s.substring(j,i)))
dp[i] = true;
}
}
return dp[s.length()];
}
HashMap<String, Boolean> hm = new HashMap();
public boolean wordBreak2(String s, List<String> wordDict) {
if(hm.containsKey(s)) return hm.get(s);
if(s.length() == 0) return true;
Boolean flag = false;
for(String word:wordDict){
if(s.startsWith(word)) flag = flag||wordBreak(s.substring(word.length()), wordDict); //注意函数 startsWith
}
hm.put(s, flag);
return flag;
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助