【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 ...
随机推荐
- 利用CFAbsoluteTimeGetCurrent()计算时间差
开发中,遇到计算时间差的问题,利用CFAbsoluteTimeGetCurrent()可以很方便的进行计算 实例: 场景:类似购物车中修改商品数量的功能,如下图所示,要求,修改完的数量,要同步到服务器 ...
- android 语音识别
Android中主要通过RecognizerIntent来实现语音识别,事实上代码比較简单.可是假设找不到设置,就会抛出异常ActivityNotFoundException.所以我们须要捕捉这个异常 ...
- BNU 34990 Justice String (hash+二分求LCP)
思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降-- #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- golang之路:mac下安装go
1.下载dkg包 2.安装 3.vim .bash_profile export GOROOT=/usr/local/goexport GOPATH=$HOME/GoglandProjects/Pro ...
- 查看/设置MySQL数据库的事务隔离级别
查看InnoDB存储引擎 系统级的隔离级别 和 会话级的隔离级别: mysql> select @@global.tx_isolation,@@tx_isolation; +---------- ...
- Matlab中使用jython扩展功能
Matlab中面向对象能力并不强,通过使用jython引擎能够对其功能扩展. 1 编辑classpath.txt增加jython.jar 在matlab中输入 which classpath.txt ...
- 近期建了一个.net源代码共享群,群共享有大量网友分享的.net(C#)商业源代码
本群创建于2013/6/21: 群里都是.net(C#)程序开发者,群共享有大量网友分享的.net(C#)商业源代码.比方:DTCMS旗舰版,hishop微分销,shopnum微分销.多用户微信公众平 ...
- 快速自检电脑是否被黑客入侵过(Linux版)
前言 严谨地说, Linux只是一个内核, `GNU Linux`才算完整的操作系统, 但在本文里还是用通俗的叫法, 把`Ubuntu`,`Debian`,`RedHat`,`CentOS`,`Arc ...
- cocos2dx 3.0rc怎样创建项目
转自官网的文档. How to Run cpp-tests on win32 In this article, I will show you how to run cpp-tests on your ...
- 【Python + Selenium】之JS定位总结
感谢:小琰子 Python+Selenium 脚本中的一些js的用法汇总: 1.滚动条 driver.set_window_size(500,500) js = "window.scroll ...