LeetCode——Word Break
Question
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s ="leetcode",
dict =["leet", "code"].
Return true because"leetcode"can be segmented as"leet code".
Solution
用动态规划求解,遍历所有长度,看当前长度i是否可以完全分化。 长度为i的字符串分为两部分,前一部分是已经算过的是否可以划分的,只需要在字典中找后面一部分是否存在。
时间复杂度为O(n^2),空间复杂度为O(n).
Code
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
vector<bool> dp(s.length() + 1, false);
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j <= i; j++) {
if (dp[j]) {
string str = s.substr(j, i - j);
if (dict.find(str) != dict.end()) {
dp[i] = true;
break;
}
}
}
}
return dp[s.length()];
}
};
LeetCode——Word Break的更多相关文章
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- 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 ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- spring中关于FeignClient的错误 QueryParam.value() was empty on parameter 1
现创建一个feignClient的接口,在其他服务调用改client时发现服务启动失败错误日志如下: org.springframework.beans.factory.BeanCreationExc ...
- c++ new(不断跟新)
1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...
- Django - 环境搭建、url、视图、模板、标签、过滤器
(一).简介 简介就不多说了,网上的内容一大堆.总结来说,django是走大而全的路线,写项目超级快,几乎什么都为你考虑到了,你就乖乖照着它的格式来写就行了. 这里来一些基本认知: web应用框架(w ...
- Leetcode-Permuation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 模态窗口原理及注意事项--http://www.alisdn.com/wordpress/?p=53
前言 在开发Windows引用程序的时候,在一些需要用户确认,或者提示用户注意的场合,经常使用模态对话框,或者叫模态窗口.在绝大多数情况下,模态窗口给开发人员带来了极大的便利,并且在某些应用上有不可替 ...
- CodeForces 156A Message(暴力)
A. Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- rest_framake之视图
开始,先放大招 一 最原始的写法 前戏之序列化 class AuthorSerializer(serializers.ModelSerializer): class Meta: model = mo ...
- 我的Android进阶之旅------>解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2
1.问题描述 今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误: java.lang.RuntimeException: Unable to start s ...
- Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)
最近遇到一个比较棘手的问题:使用GPS定位无法获取当前的地理位置,即getLastKnownLocation方法始终返回null. 后来一篇博文 getLastKnownLocation()返回n ...
- PyNest——part 3: connecting networks with synapses
part 3: connecting networks with synapses parameterising synapse models NEST提供了各种不同的突触模型. 您可以使用命令nes ...