139. Word Break(动态规划)
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[i] 0-i这个字符串是否满足wordbreak
dp[0] = 0
dp[1] =dp[0] && s[0:1] in dict
dp[2] =dp[0] && s[0:1] in dict || dp[1] && s[1:2] in dict
dp[3] =dp[0] && s[0:3] in dict || dp[1] && s[1:3] in dict || dp[2] && s[2:3] in dict
dp[i] =dp[0] && s[0:i] in dict || ,,,,,,,||dp[i-1]&& s[i-1:i] in dict
Time complexity O(n^2)
Space complexity O(n)
语法注意:
s = "abcde"; 要得到”ce“
string word = s.substr(2,2);
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.size()+,false);
unordered_set<string>dict(wordDict.begin(),wordDict.end());
dp[] = true;
for(int i = ; i<=s.size();i++){
for(int j = ;j<i;j++){
string word = s.substr(j,i-j);
if(dp[j]&&dict.find(word)!=dict.end()){
cout<<word<<endl;
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
};
参考:
http://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/
139. Word Break(动态规划)的更多相关文章
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 动态规划之139 Word Break
题目链接:https://leetcode-cn.com/problems/word-break/ 参考链接:https://blog.csdn.net/c_flybird/article/detai ...
- 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单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
随机推荐
- mongodb操作文件
mongodb操作文件,主要是通过GridFS类.存储文件主要存放在fs中,其中的fs是数据库默认的.并且GridFS是直接与数据库打交道,与collection集合无关. ============= ...
- cocoapod引入FLEX,debug模式正常,Release报错library not found for -lXXX
cocoapod引入FLEX,debug模式正常,Release报错library not found for -lXXX, 因为podfile是这么写的: pod 'FLEX', '~> 2. ...
- Altium Designer 绘图流程及快捷键
1.Shift+Ctrl+g 设置栅格捕捉大小 2.Q 切换单位 3.E+N +点击字体 改变字体大小 4.自动布线前需在Mechanical 层和keepout层添加一个边框 5.打过孔实现双面走线 ...
- Java设计模式之模板模式及使用场景
模板模式,顾名思义,就是通过模板拓印的方式. 定义模板,就是定义框架.结构.原型.定义一个我们共同遵守的约定. 定义了模板,我们的剩余工作就是对其进行充实.丰润,完善它的不足之处. 定义模板采用抽象类 ...
- 汇编-5.0-[BX]和loop指令
1.要完整的描述一个内存单元,需要两种信息:1.内存单元的地址:2.内存单元的长度(类型). 2."()"表示一个寄存器或一个内存单元中的内容.如:(ax)表示ax中的内容. &q ...
- 浏览器的cookie的值改成字典格式
首先我们把复制的cookie的值赋给b >>> cookies = 'bid=Qzw9cKnyESM; ll="108288"; __yadk_uid=4YChv ...
- WordPress如何屏蔽恶意关键词搜索
我们在用WordPress建站比较方便,但如果网站有一定的权重后,一些不怀好意的人就会过来制作恶意内容,比如故意搜索邪恶的关键词.垃圾评论等,那我们如何屏蔽恶意搜索关键词呢?不会很难,会写点代码的朋友 ...
- VS 2017 安装测试
3月7日, VS 出了新的版本2017 安装效果如下: 不过官方说会改变VS 2015附件python 的方式,变成类似C++ 一样集成python语言包到VS中. 目前没有看见,大家如果有兴趣可以安 ...
- 如何用python发邮件
python发送各类邮件的主要方法 一.相关模块介绍 发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍: 1.smtplib模块 smtplib.SM ...
- Python3学习之路~6.1 编程范式:面向过程 VS 面向对象
编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...