Leetcode#139 Word Break
与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些。
依然是动态规划。
代码:
bool wordBreak(string s, unordered_set<string> &dict) {
int maxLen = ;
for (auto w : dict)
maxLen = maxLen > w.length() ? maxLen : w.length(); vector<bool> res(s.length() + , false);
res[s.length()] = true; for (int i = s.length() - ; i >= ; i--) {
for (int l = ; !res[i] && l <= maxLen && i + l <= s.length(); l++)
res[i] = dict.find(s.substr(i, l)) != dict.end() && res[i + l];
} return res[];
}
Leetcode#139 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 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 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode #139. Word Break C#
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- Java for LeetCode 139 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- 多屏判断css改写
function replaceBodyClass(){ for(var i in map){ if(map[i](width)){ document.body.className = documen ...
- text-rendering 详解
原文链接:http://www.feeldesignstudio.com/2013/05/text-rendering Text-rendering 属性是一个非标准属性,主要用来告诉渲染引擎(ren ...
- 为php安装memcached扩展连接memcached服务器
首先必须完成必要的软件安装,memcached是php连接memcached服务器的php扩展 以前有个叫memcache也是php连接memcached服务器的扩展 php的memcache和mem ...
- PHP系统函数
(一)字符串处理函数 Chr函数 作用:根据ASCII码返回相应的字符. 语法:string chr(int ascii): Chop函数 作用:去除字符串中连续空格和空白行. 语法:string c ...
- Delphi For Android 开发笔记 2 NEXTGEN下的字符串类型
delphi开发速度迅捷至少有30%(猜的,呵呵)的原因是因为其字符串(string.WideString.PChar.PAnsiChar等)处理能力. 而从delphi XE4开始,在system等 ...
- delphi启动 EditLineEnds.ttr 被占用问题
碰到这个问题我也是无语了,每次关掉Delphi后就不能打开了,折腾了半天,网上的方法也没有搞定.最后,找到这个链接(网页如下所示),下载里面的东西就搞定了.打不开的可以向我要.895569369@qq ...
- asp.net解析请求报文
NameValueCollection myHeader = new NameValueCollection(); int i; string strKey; string result; myHea ...
- Python生成器以及yield语句
生成器是一种暂缓求值的技术,它可以用来生成一系列的值,但不会一次性生成所有的值,而只在需要的时候才计算和生成一个值. 通过yield语句构建生成器 要得到一个生成器,我们需要定义一个函数,这个函数返回 ...
- DrawTool多重笔之前奏 => 通过InkAnalyzer实现图形识别
这里要介绍的是通过InkAnalyzer来实现简单图形的识别,例如圆,椭圆,正方形,三角形等,当然你也可以通过扩展来实现自定义图形的识别,在使用InkAnalyzer前,你需要引用IAWinFX.dl ...
- 欢迎来到vmax-tam的博客
欢迎来到vmax-tam的博客 我是一个新手程序员 以后会不断学习 把学到的东西记录下来 和大家一起分享的 谢谢大家指教