leetcode笔记:Word Break
一. 题目描写叙述
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"
.
二. 题目分析
假设使用递归,会超时。
这时使用动态规划就可以解决这个问题,即将源字符串s从開始到结尾。分解成各个子串进行操作,对于这类字符串组合问题,须要掌握相似状态转移方程。
对于下标i
所相应字符的匹配状态flag[i]
,假设dict有字符串能够匹配,这取决于之前某个字符j
的状态出现匹配。且从数组s的j + 1
到i
下标之间的字符也能从dict中找到匹配的字符串:
flag[i] = any(flag[j] && (s[j + 1, i] ∈ dict))
三. 演示样例代码
class Solution
{
public:
bool wordBreak(string s, unordered_set<string> &dict)
{
vector<bool> wordFlag(s.size() + 1, false); // 动态规划
wordFlag[0] = true;
for (int i = 1; i < s.size() + 1; ++i)
{
for (int j = i - 1; j >= 0; --j)
{
if (wordFlag[j] && dict.find(s.substr(j, i - j)) != dict.end())
{
wordFlag[i] = true;
break;
}
}
}
return wordFlag[s.size()];
}
};
四. 小结
动态规划对于解决一些字符串的问题也是有效且easy实现的。
leetcode笔记:Word Break的更多相关文章
- [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 Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- [Leetcode Week9]Word Break
Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...
- 【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 ...
随机推荐
- Hexo 添加自定义的内置标签
灵感 想设计一个记录自已骑行的页面,显示时间.地点.路线图等信息.方便以后做一些留念.定位想实现下面类似的效果.参考:<特效> 实现方案也比较简单,反键查看源码.直接Copy,在 ...
- 注解:@SuppressWarning()的用法
@SuppressWarning() 作用:J2SE 提供的一个批注或者注解.该批注的作用是给编译器一条指令,忽略这些警告信息. 常用:unchecked,serial. 1.如果传入多种情况,这几种 ...
- 超好用的谷歌浏览器、Sublime Text、Phpstorm、油猴插件合集
原文:超好用的谷歌浏览器.Sublime Text.Phpstorm.油猴插件合集 - 『精品软件区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|破解软件|www.52pojie.c ...
- oracle 正序 逆序 排序查询
正序:从小到大 order by t.id asc 逆序:从大到小 order by t.id desc
- codevs——T1006 等差数列
http://codevs.cn/problem/1006/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- 洛谷 P2684 搞清洁
P2684 搞清洁 题目描述 FJ准备分配它的N只奶牛(1 <= N <= 25,000) 做清洁工作,他把一天分成T(1 <= T <= 1,000,000)个时间段,他希望 ...
- iOS 开发之IPad的设计与实现
// // main.m // 6-ipad // #import <Foundation/Foundation.h> #import "Ipad.h" int mai ...
- hdu5024
思路要开阔些,或者说要转化一下思路,别太死 把每一个点当拐点,爆一边就能够.用记忆化搜索也行.都不会超时 #include<bits/stdc++.h> using namespace s ...
- elasticsearch index 之merge
merge是lucene的底层机制,merge过程会将index中的segment进行合并,生成更大的segment,提高搜索效率.segment是lucene索引的一种存储结构,每个segment都 ...
- html --- bootstrap 框架 (栅格系统布局)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...