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 ...
随机推荐
- mysql省市区数据库表源码
下载 一:创建表 1省: create table CREATE TABLE `provinces` ( `id` ) NOT NULL AUTO_INCREMENT, `provinceid` ...
- COGS——T 7. 通信线路
http://www.cogs.pro/cogs/problem/problem.php?pid=7 ★★ 输入文件:mcst.in 输出文件:mcst.out 简单对比时间限制:1.5 ...
- 使用Linux遇到的一些问题和解决方案
1.如何在重装系统或换机器以后继续使用以前用过的thunderbird邮件 执行命令thunderbird -ProfileManager以后会打开一个配置用户界面. 在里面添加一个新的配置,然后选择 ...
- css+ js 实现圆环时钟
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 图片的title属性和alt属性的区别
在前端开发中,经常遇到有人在问图片的alt属性和title属性的区别,这是因为很多人对于alt属性和title属性没有彻底搞明白,今天零度给大家分析分析. title属性 首先,来看一下什么是titl ...
- 70.lambda表达式逻辑(二进制转换为为十进制)
#include <iostream> #include <cstring> using namespace std; void main() { auto fun = []( ...
- 多行文本溢出显示...的方法(-webkit-line-clamp)
限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. 为了实现该 ...
- Python Web框架要点
1. Web应用程序处理流程 前端客户端(浏览器.APP.ajax.爬虫程序)>>>--http的请求与响应--<<<服务器程序(Gunicorn.uwsig)&l ...
- C# Unable to load DLL 'WzCanDll.dll':找不到指定的模块
一.打开app无法加载DLL 我用C++编写的DLL,然后用C#写的界面APP,在自己的电脑上打开没有问题,放在其它电脑上就出现无法加载DLL库的问题,一连接APP就会出现问题,如下图所示: 二.解决 ...
- 【2017 Multi-University Training Contest - Team 4】Counting Divisors
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6069 [Description] 定义d(i)为数字i的因子个数; 求∑rld(ik) 其中l,r ...