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 ...
随机推荐
-
ShellExecute调用另外一个进程(demo为一个控制led的一段代码)
public enum ShowCommands : int { SW_HIDE = 0, SW_SHOWNORMAL = 1, ...
-
bootstrap如何给.list-group加上序号
在bootstrap中,我们可以使用不带任何class的<ol>跟<li>来创建一个有序列表,但是如果加上list-group类,样式有了,但列表前面的数字却没了. Boots ...
-
Lua面向对象编程
Lua中的table就是一种对象,看以下一段简单的代码: , b = } , b = } local tb3 = tb1 if tb1 == tb2 then print("tb1 == t ...
-
json格式的转换为json字符串函数
function toJSON(object){ var type = typeof object; if ('object' == type) { if (Array == object.const ...
-
(转)ObjC利用正则表达式抓取网页内容(网络爬虫)
转自:http://www.cocoachina.com/bbs/read.php?tid=103813 *****boy]原创 2012年5月20日 在开发项目的过程,很多情况下我们需要利用互联网上 ...
-
windows下php开发环境的搭建
环境搭建软件组合为:Apache2.2.9+mysql5.2.32+php5.2.6 下载地址如下 http://download.csdn.net/detail/xttxqjfg/5670455 ...
-
SD
Offer(Tcode:VA23;Table: vbak and vbap) billing(Tcode:VF03;Table:vbrk and vbrp) Offer(quotation)-> ...
-
分布式缓存技术memcached学习系列(三)——memcached内存管理机制
几个重要概念 Slab memcached通过slab机制进行内存的分配和回收,slab是一个内存块,它是memcached一次申请内存的最小单位,.在启动memcached的时候一般会使用参数-m指 ...
-
HDU6298 Maximum Multiple (多校第一场1001)
Maximum Multiple Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
-
03 Java 修饰符
Java 修饰符主要分为两类: 访问修饰符 非访问修饰符 访问修饰符 public,对所有类可见 protected,对同一包内的类和子类可见 default,同一个包内的类可见 private,对当 ...