[Leetcode Week9]Word Break
Word Break 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/word-break/description/
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. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
Solution
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int i, j;
int size = s.length();
vector<bool> flags(size + 1, false);
flags[0] = true;
for (i = 0; i <= size; i++) {
for (j = 0; j <= i - 1; j++) {
if (flags[j] && find(wordDict.begin(), wordDict.end(), s.substr(j, i - j)) != wordDict.end()) {
flags[i] = true;
break;
}
}
}
return flags[size];
}
};
解题描述
这道题采用的是动态规划的方式进行求解。将整个问题化成小问题:生成来源字符串的所有子串,并在词典中查找字串,如果存在则进行标记,之后查看所有的这些字串能否连起来组成原来的字符串。
[Leetcode Week9]Word Break的更多相关文章
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- leetcode 140. Word Break II ----- java
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- Thymeleaf 使用时的标签
1 . onclick事件 <a th:onclick="'javascript:more()'" ></a> 2.引入CSS样式 <link t ...
- python 网络篇(网络编程)
一.楔子 你现在已经学会了写python代码,假如你写了两个python文件a.py和b.py,分别去运行,你就会发现,这两个python的文件分别运行的很好.但是如果这两个程序之间想要传递一个数据, ...
- mysqldump: Got error: 1135: Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug when trying to connect 解决办法
在进行数据库备份的时候发现服务器报 mysqldump: Got error: 1135: Can't create a new thread (errno 11); if you are not o ...
- python中字典的循环遍历的两种方式
开发中经常会用到对于字典.列表等数据的循环遍历,但是python中对于字典的遍历对于很多初学者来讲非常陌生,今天就来讲一下python中字典的循环遍历的两种方式. 注意: python2和python ...
- DPDK Qos之报文处理流水线
原创翻译,转载请注明出处. 下面是一个支持Qos的复杂报文处理流水线的图: 流水线是通过DPDP可重用的软件库构建出来的.在流水线里实现QoS主要是如下模块:policer,dropper,shced ...
- js阻止冒泡事件和默认事件的方法
阻止默认事件 function stopDeFault(e){ if(e&&e.preventDefault){//非IE e.preventDefault(); }else{//IE ...
- online community
online community spectrum https://spectrum.chat/xgqfrms https://community.xgqfrms.xyz/ https://spect ...
- javascript string对象方法replace
最简单的replace用法是: var str = 'aaaaa9876b0000'; str.replace(/a/g,'A'); 有时候我们希望只是在匹配的位置添加特定的字符: var str = ...
- sessionStorage的用法总结
sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁.因此sessionStorage不是一种持久化的本地 ...
- hdu 3500 Fling (dfs)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...