作者: 负雪明烛
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的路子来:

  1. 确定可以保存的信息
  2. 递推式(以及如何在递推中使用保存的信息)
  3. 确定起始条件

放到这个题说

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++)的更多相关文章

  1. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  2. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  3. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  4. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  5. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

随机推荐

  1. R 语言 select函数在org.Hs.eg.db上的运用

    首先org.Hs.eg.db是一个关于人类的 一,在R中导入包library(org.Hs.eg.db) http://www.bioconductor.org/packages/release/da ...

  2. 8种Vue中数据更新了但页面没有更新的情况

    目录 1.Vue 无法检测实例被创建时不存在于 data 中的 属性 2. Vue 无法检测'对象属性'的添加或移除 3.Vue 不能检测利用数组索引直接修改一个数组项 4.Vue 不能监测直接修改数 ...

  3. java输入代码

    import java.util.Scanner; public class Demo59 {    public static void main(String[] args) {        / ...

  4. 如何通过 User-Agent 识别百度蜘蛛

    如果有大量的百度蜘蛛抓取网站就需要注意了:有可能是其他爬虫伪造百度蜘蛛恶意抓取网站. 如果遇到这种情况,这时候就需要查看日志来确定是不是真正的百度蜘蛛(baidu spider).搜索引擎蜘蛛.用户访 ...

  5. 【STM32】使用DMA+SPI传输数据

    DMA(Direct Memory Access):直接存储器访问 一些简单的动作,例如复制或发送,就可以不透过CPU,从而减轻CPU负担 由于本人使用的是正点原子开发板,部分代码取自里面的范例 本篇 ...

  6. java_IO总结(一)

    所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后) 对于文件内容的操作 ...

  7. RAC中常见的高级用法-组合

    组合: concat组合:           按一定顺序执行皇上与皇太子关系 concat底层实现:     1.当拼接信号被订阅,就会调用拼接信号的didSubscribe     2.didSu ...

  8. 通过js禁用浏览器的回退事件

    js代码: <script> history.pushState(null, null, document.URL); window.addEventListener('popstate' ...

  9. Redis图形管理 redis-browser

    目录 一.介绍 二.部署 三.启动 监听单台 听多台 四.报错合集 一.介绍 redis-browser是redis的web端图形化管理工具.利用它可以查看和管理redis的数据,界面简洁,能和ral ...

  10. Samba 源码解析之SMBclient命令流

    smbclient提供了类似FTP式的共享文件操作功能, 本篇从源码角度讲解smbclient的实现,smbclient命令的具体使用可通过help命令和互联网查到大量资料. 以下从源码角度分析一个s ...