leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
查看字符串是否由字典中的单词组成。
1、暴力递归,果断超时。
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
return helper(s,0,wordDict);
}
public boolean helper(String s,int start,Set<String> wordDict ){
if( start == s.length() )
return true;
for( int i = s.length();i > start ;i--){
if( wordDict.contains( s.substring(start,i) ) ){
if( helper(s,i,wordDict) )
return true;
}
}
return false;
}
}
2、dp,基本达到最快。
int len = s.length(),maxLen = 0;
boolean[] dp = new boolean[len];
for( String str : wordDict ){
maxLen = Math.max(maxLen,str.length());
} for( int i = 0 ;i<len;i++){ for( int j = i;j>=0 && i-j<maxLen;j-- ){
if( ( j == 0 || dp[j-1] == true ) && wordDict.contains(s.substring(j,i+1)) ){
dp[i] = true;
break;
}
}
} return dp[len-1];
leetcode 139. Word Break ----- java的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- Java for LeetCode 139 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- LeetCode #139. Word Break C#
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- 【模拟题(电子科大MaxKU)】解题报告【树形问题】【矩阵乘法】【快速幂】【数论】
目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s ...
- 【C语言学习】-05 二维数组、字符串数组、多维数组
⼆二维数组.字符串数组.多维数组
- 编写一个小Servlet程序
1.编写一个java类,此类继承HttpServlet 创建完工程(见上一篇随笔:使用eclipse创建在myeclipse中运行的web工程),在src中新建一个包,包名字叫servlet:再新建一 ...
- PC客户端测试总结
1.1界面显示内容的检查l 完整性(1显示时应考虑数据显示宽度的自适应或自动换行(数据长度较长).(2所数据展现的界面(如查询等),必须使测试数据的记录数超过一页,以验证满页时其窗体是否有横向.纵向滚 ...
- 【kate总结】Matlab坐标轴问题
[kate总结]Matlab坐标轴问题 总结而言 行óYó高ó垂直 列óXó宽ó水平 Maplab中存有2张图片 1.JPG 宽(列):320 高(行):482 在matlab中显示 2.JPG ...
- idea使用generator自动生成model、mapper、mapper.xml(转)
原文链接:http://www.mamicode.com/info-detail-445217.html TEP 0.在Intellij IDEA创建maven项目(本过程比较简单,略) STEP 1 ...
- SharePoint 列表应用实例 - 显示约束
博客地址:http://blog.csdn.net/FoxDave 有时会碰到这样的需求,比如上传周报到文档库,周报只能领导和自己看到,其他同事是看不到的.通常我们开发的人遇到这种情况条件反射地想到的 ...
- PHP面向对象的继承
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 深入分析:Fragment与Activity交互的几种方式(三,使用接口)
第一步:我们需要在Fragment中定一个接口,并确保我们的容器Activity实现了此接口: public interface onTestListener { public void onTest ...
- Linux怎么使用添加的新硬盘
一.磁盘分区 装过系统后第一块磁盘的设备号是/dev/sda,在你添加一个新的磁盘后一般情况下是/dev/sdb *******进入fdisk界面***** # fdisk /dev/sdbDevic ...