【LeetCode】139. Word Break 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/unique-binary-search-trees/description/
题目描述
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".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
题目大意
判断一个字符串能不能由给定的字典中的字符串拼接得到。
解题方法
正好又学习巩固了一下DP。感觉DP套路太多了,每道题都不一样,至少LC上是这样的,很难说总结出什么规律。
按照CodeGanker的路子来:
- 确定可以保存的信息
- 递推式(以及如何在递推中使用保存的信息)
- 确定起始条件
放到这个题说
S能拆成功的话,说明
s[0:k]能拆成功,然后 s[k:i]是一个在字典中的单词。
后者是一步check: s[k:i] in wordDict;
前者是需要记录的信息dp[k]表示可拆
然后从头撸一遍就行了
有的时候,一个题自己不明白,看了别人的答案还是不懂,但是看了运行的结果就行。
"leetcode"
[u'leet', u'code']
[True, False, False, False, False, False, False, False, False]
[True, False, False, False, True, False, False, False, False]
[True, False, False, False, True, False, False, False, True]
"leetcode"
[u'le', u'et', u'code']
[True, False, False, False, False, False, False, False, False]
[True, False, True, False, False, False, False, False, False]
[True, False, True, False, True, False, False, False, False]
[True, False, True, False, True, False, False, False, True]
"leetcode"
[u'l', u'ee', u't', u'co', u'd', u'e']
[True, False, False, False, False, False, False, False, False]
[True, True, False, False, False, False, False, False, False]
[True, True, True, False, False, False, False, False, False]
[True, True, True, True, False, False, False, False, False]
[True, True, True, True, False, False, False, False, False]
[True, True, True, True, True, False, False, False, False]
[True, True, True, True, True, False, True, False, False]
[True, True, True, True, True, False, True, True, False]
[True, True, True, True, True, False, True, True, True]
做DP的题目一定要明白定义的dp[i]到底是什么,这个题里面的dp[i]代表的是[0,i)符不符合word break。需要遍历的范围就是从0~N+1. dp[0]是空字符串,就是true.
其实这个题和416. Partition Equal Subset Sum很像的,都是两重循环,第一重循环判断每个位置的状态,内层循环判断这个状态能不能有前面的某个状态+一个符合题目要求的条件得到。
Python代码:
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
print(s)
print(wordDict)
dp = [False] * (len(s) + 1)
dp[0] = True
print(dp)
for i in xrange(1, len(s) + 1):
for k in xrange(i):
if dp[k] and s[k:i] in wordDict:
dp[i] = True
print(dp)
return dp.pop()
这个题的C++代码如下:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
const int N = s.size();
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
// dp[i] means s[0:i) is wordBreak or not.
vector<bool> dp(N + 1, false);
dp[0] = true;
// i in range [0, N)
for (int i = 0; i <= N; ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j] && wordSet.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp.back();
}
};
日期
2018 年 2 月 25 日
2018 年 12 月 18 日 —— 改革开放40周年
2019 年 1 月 10 日 —— 加油
【LeetCode】139. Word Break 解题报告(Python & C++)的更多相关文章
- 【LeetCode】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 单词拆分
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 ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- 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 ----- 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单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
随机推荐
- perl和python3 同时打开两个文件
perl : while(defined(my $f1=<FQ1>) && defined(my $f2=<FQ2>)){ condition } python ...
- excel--CLEAN()函数,解决为什么看着相同的字符串但是len()长度不同
CLEAN()函数能够有效解决去除字符串中隐藏的字符(这些字符是TRIM()去除不掉的)
- Shell 打印文件的最后5行
目录 Shell 打印文件的最后5行 题解-awk 题解-tail Shell 打印文件的最后5行 经常查看日志的时候,会从文件的末尾往前查看,于是请你写一个 bash脚本以输出一个文本文件 nowc ...
- postgresql安装部署
一.下载安装: 1.下载: 官网下载地址:https://www.postgresql.org/download/linux/redhat/ 也可以用这个:https://www.enterprise ...
- Kafka入门教程(二)
转自:https://blog.csdn.net/yuan_xw/article/details/79188061 Kafka集群环境安装 相关下载 JDK要求1.8版本以上. JDK安装教程:htt ...
- 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户
1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...
- OC-ARC,类扩展,block
总结 标号 主题 内容 一 autorelease autorelease基本概念/自动释放池/autorelease基本使用 二 autorelease注意事项 注意点/应用场景 三 ARC 什么是 ...
- Spring Boot项目的探究
一.pom.xml文件 1.父项目 <parent> <groupId>org.springframework.boot</groupId> <artifac ...
- 【Windows】github无法访问/hosts文件只能另存为txt
因为我的github访问不了了,搜索解决方案为修改host文件 https://blog.csdn.net/curry10086/article/details/106800184/ 在hosts文件 ...
- Apache Log4j2远程代码执行漏洞攻击,华为云安全支持检测拦截
近日,华为云安全团队关注到Apache Log4j2 的远程代码执行最新漏洞.Apache Log4j2是一款业界广泛使用的基于Java的日志工具,该组件使用范围广泛,利用门槛低,漏洞危害极大.华为云 ...