[leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
题意:
给定字符串S和字典wordDict, 判断字符串S是否能由字典中的单词组合而成
思路:
用一维boolean数组记录是否能字符串S是否能被分隔
l e e t c o d e
| T | F | F | F | F | F | F | F | F |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
dp[0]初始化为true, 其他为default的false
dp[s.length() + 1]
i = 1 j = 1, dp[1] 为default F, 没必要再去验证wordDict.contains(s.substring(j,i))
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,1)) 为F
i = 2 j = 2, dp[2] 为default F
j = 1, dp[1] 为default F
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,2)) 为F
i = 3 j = 3, dp[3] 为default F
j = 2, dp[2] 为default F
j = 1, dp[1] 为default F
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,3)) 为F
i = 4 j = 4, dp[4]为default F
j = 3, dp[3] 为default F
j = 2, dp[2] 为default F
j = 1, dp[1] 为default F
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,4)) 为T ----> 更新dp[4] = true
| T | F | F | F | T | F | F | F | F |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
代码:
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
// corner case
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
//外层循环scan字符串S
for(int i = 1; i <= s.length(); i++ ){
//内层循环找分割点
for(int j = i; j >= 0; j--){
if(dp[j] && wordDict.contains(s.substring(j,i))){
dp[i] = true;
}
}
}
return dp[s.length()];
}
}
[leetcode]139. Word Break单词能否拆分的更多相关文章
- [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单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 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 ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 139 Word Break 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...
- leetcode 139. Word Break ----- java
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 string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
随机推荐
- [UE4]目标是Pawn、Get Player Character
“目标是Pawn”表示这一个定义继承与Pawn类的方法. 这样可以很清楚的看到这个是方法是在什么地方定义的 “Get Player Character”可以获得当前控制的角色实例,可以转换成真正具体的 ...
- [Windows]Win10下VM虚拟机桥接模式无法上网的解决办法
Win10出来了,赶紧尝尝鲜.既然是预览版,肯定会出现以前没有过的问题.这不,问题马上就来了.我的VM虚拟机本来在Win8.1下使用桥接模式是可以上网的,但是现在不可以了.重置了好几次虚拟网络,NAT ...
- mysql 字符集排查
mysql 字符集排查 库级别 SELECT * FROM information_schema.schemata WHERE schema_name NOT IN ( 'information_sc ...
- python的return self的用法
转载:https://blog.csdn.net/jclian91/article/details/81238782 class foo: def __init__(self): self.m = 0 ...
- API网关Kong系列(一)初识
最近工作需要,加上国内Kong的文章相对缺乏(搜来搜去就那么两篇文章),而且官方文档在某些demo上也有一些过时的地方,遂提笔记录下这些,希望能有帮助. 先随大流介绍下KONG(主要参考官网): 官方 ...
- 【基础知识六】支持向量机SVM
开发库: libsvm, liblinear GitHub地址 SVM难点:核函数选择 一.基本问题 找到约束参数ω和b,支持向量到(分隔)超平面的距离最大:此时的分隔超平面称为“最优超平面 ...
- IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理
来源:http://www.guchengnet.com/1499.html IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理 发表于2016年12月14日 有2.3个月没有用本地的i ...
- nms
nms函数是保留选框中得分最高的那一个 Python代码如下 def nms(boxes, threshold, method): """ boxes: [x1, y1, ...
- 28. centos 5.6添加用户时报copydir(): preserving permissions for /home/xxx/.mozilla: Operation not supported错
当执行useradd xxx报如下错:copydir(): preserving permissions for /home/xxx/.mozilla: Operation not supported ...
- docker学习笔记 参考
https://www.cnblogs.com/YDDMAX/p/6045079.html 参考此人播客:docker 分类 http://www.cnblogs.com/51kata/categor ...