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 ...
随机推荐
- SortedMap与TreeMap的一个典型应用
一下是在项目中的应用. msg.getContent()共有四种类型. public SortedMap<String, List<ActivityMsg>> queryTri ...
- bat实现获取文件每行内容,for循环中运行多条命令
关键词:bat,bat获取文件内容 1.获取每行内容 @echo offfor /f "delims=" %%i in (config.txt) do (echo "%% ...
- finecms如何批量替换文章中的关键词?
Finecms批量替换文章关键词要怎么操作呢,比如把关键词A换为B?Finecms是免费开源无商业限制的内容管理系统,个人在维护,但二次开发很灵活,我们可以通过开发插件或数据库sql语句来操作,下面就 ...
- ITouch在xcode下提示‘No such file or directory, at ‘/SourceCache/DVTi...'
版权声明:本文为博主原创文章,转载或又一次发表请先与我联系. https://blog.csdn.net/jonahzheng/article/details/37692733 用一个台老 ...
- 限制SSH用户访问Linux中指定的目录
限制SSH用户访问Linux中指定的目录 http://os.51cto.com/art/201703/534895.htm#topx http://www.cnblogs.com/lykyl/arc ...
- what' the python之面向对象(进阶)
面向对象的知识点补充(进阶版) classmethod和staticmethod:这两个函数的用途就是可以不用实例化对象就可以调用方法 class Classmethod_Demo(): role = ...
- equals和contains的区别
equals只能判断两个变量的值是否相等.contains常用与集合中判断某个对象是否含有这个元素equals是需要两个对象完全相同才会返回true,而contains是要循环遍历容器里的所有内容后判 ...
- [硬件]Robot运动控制
思考问题:机器人运动控制如何与图形界面交互? 不得不说,先锋机器人的软件做的真不怎么样.图形界面交互用户体验很差. 现在我遇到一个很现实的问题:SLAM需要采集激光数据和机器人的位姿,同时我还要再这个 ...
- 常用笔记:Web前端
[HTML] <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...
- Ngx_Lua使用分享
2017年04月22日 20:05:21 阅读数:430 Nginx_Lua 1.1. 介绍 1.2. 安装 1.2.1. 安装JIT平台 1.2.2. NDK与Lua_module 1.2.3. 编 ...