LeetCode 139. Word Break单词拆分 (C++)
题目:
Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, 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
分析:
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
如果使用搜索的话应该是会超时的,这里使用动态规划的方法。
dp[i]表示字符串s的前i个字符能否被拆分,使用substr来截取字符串。
| l | e | e | t | c | o | d | e | ||
| dp | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ? |
假如我们现在判断dp[i]的值,我们要同时判断dp[j]==1和后面的字符串(substr(j, i-j))是否在字典中是不是同时成立的。
比如现在要求dp[8]的值,j=0时,dp[0]==1但substr(0, 8-0)也就是leetcode不在字典中,当j=4时,dp[4]==1且substr(4, 8-4)也就是code在字典中,所以dp[8]赋值1。最后返回dp中最后的值即可。
| l | e | e | t | c | o | d | e | ||
| dp | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
注:在dp的长度要比字符串长度多1,是为了方便计算。
程序:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> set_s(wordDict.begin(), wordDict.end());
vector<int> dp(s.size()+, );
dp[] = ;
for(int i = ; i < dp.size(); ++i){
for(int j = ; j <= i; ++j){
string it = s.substr(j, i-j);
if(set_s.count(it) && dp[j]){
dp[i] = ;
break;
}
}
}
if(dp.back()) return true;
else return false;
}
};
LeetCode 139. Word Break单词拆分 (C++)的更多相关文章
- [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单词能否拆分
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 、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 ...
- Leetcode139. Word Break单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...
- 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 ...
随机推荐
- 20K掌握的技术要点?
银四指的是每年的三四月份都是人才招聘的高峰期,由于跟新年和春运紧接,到人才市场,人都是满的,所以称为 :伴随的四月则称为银四.每一年职场迎来“ 银四”.总结做完了,得失看清了,奖金拿到了,“算账”往后 ...
- node爬虫之图片下载
背景:针对一些想换头像的玩家,而又不知道用什么头像的,作为一名代码爱好者,能用程序解决的,就不用程序来换头像,说干就干,然后就整理了一下. 效果图 环境配置 安装node环境 node -v node ...
- QTreeWidgetItem清空子节点
下面列出,xxbs遇到的注意点儿: 1. QTreeWidget::collapseAll(); //xxbs::先折叠所有根项. 如果某个根是展开的,先删除根的子项再折叠,展开的凸显状态角色无法清除 ...
- ThreadLocal 简单解析
ThreadLocal 简单解析 基于jdk1.8 ThreadLocal一定不陌生,开发中常用,也是面试里的常客了,但是往往我们可能只是知道该类的作用.学习该类对于个人的多线程编码能力是大有裨益的, ...
- LeetCode 141:环形链表 Linked List Cycle
给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a l ...
- 使用selenium爬虫抓取数据
写在前面 本来这篇文章该几个月前写的,后来忙着忙着就给忘记了.ps:事多有时候反倒会耽误事.几个月前,记得群里一朋友说想用selenium去爬数据,关于爬数据,一般是模拟访问某些固定网站,将自己关注的 ...
- MySQL管理工具 -- MySQL Workbench
管理MySQL,可以使用可视化图形界面MySQL Workbench.MySQL Workbench是一个图形客户端,可以用可视化的方式查询.创建和修改数据库表.它对MySQL的操作仍然是发送SQL语 ...
- Kubernetes 动态PV使用
Kubernetes 动态PV使用 Kubernetes支持动态供给的存储插件:https://kubernetes.io/docs/concepts/storage/storage-classes/ ...
- yum 找不到程序,yum更换国内阿里源
使用百度云服务器,发现百度yum源非常不稳定,果断采用阿里源,操作步骤如下: 一.备份 $ cd /etc/yum.repos.d/ $ mv baidu-bcm.repo baidu-bcm.rep ...
- 配置 ASP.NET Core 请求(Request)处理管道
配置 ASP.NET Core 请求(Request)处理管道 在本节中,我们将讨论使用中间件组件为 asp.net core 应用程序配置请求处理管道. 作为应用程序启动的一部分,我们要在Confi ...