【leetcode】Word Break
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"
.
题解:动态规划
设置数组dp,dp[i,j]表示从s的第i个字母(i=0~s.length-1)开始,长度为j的子字符串是否在字典dict中。则,有以下递推式:
- 如果字典包含子字符串s[i,i+j-1],则 dp[i,j] = true;
- 如果字典包含子字符串s[i,i+k-1]和字符串s[i+k,i+j],则dp[i,j] = true;
- 否则,dp[i,j] = false;
代码如下:
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if(s == null || dict.size() == 0)
return false;
int length = s.length();
boolean[][] dp = new boolean[length][length+1]; for(int len = 1;len <= length;len++){
for(int i = 0;i+len <= length;i++){
String sub = s.substring(i,i+len);
if(dict.contains(sub)){
dp[i][len] = true;
continue;
} for(int k = 1;k < len;k++){
if(dp[i][k] && dp[i+k][len-k] )
{
dp[i][len] = true;
break;
}
}
}
} return dp[0][length];
}
}
对于字符串,substring这个函数返回的是beginIndex和endIndex-1之间的子字符串!
【leetcode】Word Break的更多相关文章
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【leetcode】Word Break(python)
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...
- 【leetcode】Word Break II (hard)★
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 【Leetcode】【Medium】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
随机推荐
- 初探J2EE
还记得在技术交流会上八期给我们讲的J2EE,当时就是云里来屋里去.留在自己脑子中的仅仅有两个字"规范",其他的真是一无全部. 可是如今学了后,又在脑子里留下了两个字"规范 ...
- Android中database所在文件夹路径(9.6)
1 sd----->data---->对应app---->databases----->创建的db 2 push到pc上,可以使用GUI工具SQLiteSpy直接查看datab ...
- SlidingMenu+Fragment实现当前最流行的侧滑
1 http://www.krislq.com/2013/03/android_case_slidingmenu_fragment/ 2 https://github.com/jfeinstein10 ...
- PHP面试题及答案解析(4)—PHP核心技术
1.写出一个能创建多级目录的PHP函数. <?php /** * 创建多级目录 * @param $path string 要创建的目录 * @param $mode int 创建目录的模式,在 ...
- Fiddler 默认不能抓取页面信息的问题
先如下配置
- weex 学习
相关资料和链接: # 官方网站https://weex.apache.org/cn/ # githubhttps://github.com/apache/incubator-weex # weex环境 ...
- linux系统下面ftp的一些命令
service vsftpd restart重启vsftpd服务service vsftpd stop停止vsftpd服务service vsftpd start启动vsftpd服务 chkconfi ...
- hashmap的equal和hashcode为什么要同时重写
如果你重载了equals,比如说是基于对象的内容实现的,而保留hashCode的实现不变,那么很可能某两个对象明明是“相等”,而hashCode却不一样. 这样,当你用其中的一个作为键保存到hashM ...
- elk文件
=================正则匹配 [root@web02 conf.d]# cat apache-grok.conf input{ file { path => "/var/ ...
- hibernate 注解之 SequenceGenerator
hibernate 注解之 SequenceGenerator https://blog.csdn.net/zgf19930504/article/details/54694807 JPA @Id 和 ...