[LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址:
https://leetcode.com/problems/word-break/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".
解法:
这道题目利用动态规划做出来,不得不说想法是很巧妙的,我也是参考了网上的代码才AC了。因此,先放代码,等我完全弄懂再补充吧:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if(s == "" || s.size() == ) {
return true;
}
unordered_map<int, bool> res;
for (int i = ; i <= s.size(); i++) {
res[i] = false;
}
res[] = true;
for (int i = ; i < s.size(); i++) {
string str = s.substr(, i + );
for (int j = ; j <= i; j++) {
if (res[j] && find(wordDict.begin(), wordDict.end(), str) != wordDict.end()) {
res[i + ] = true;
break;
}
str = str.substr(, str.size() - );
}
}
return res[s.size()];
}
};
2018.1.7更新
另外的做法(其实就是换了一种统计层数的方法):
class Solution {
public:
bool isConnected(string a, string b) {
int num = ;
for (int i = ; i < a.size(); i++) {
if (a[i] != b[i]) num++;
}
return num == ;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int res = ;
queue<string> s;
s.push(beginWord);
while (!s.empty()) {
int size = s.size();
for (int i = ; i < size; i++) {
string str = s.front();
s.pop();
if (str == endWord) {
return res;
}
for (vector<string>::iterator iter = wordList.begin(); iter != wordList.end();) {
if(isConnected(str, *iter)) {
s.push(*iter);
iter = wordList.erase(iter);
} else {
iter++;
}
}
}
res++;
}
return ;
}
};
[LeetCode] 139 Word Break(BFS统计层数的方法)的更多相关文章
- [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
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 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 拆分词句
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 ...
随机推荐
- 为什么要使用自增ID作为主键
1.从业务上来说 在设计数据库时不需要费尽心思去考虑设置哪个字段为主键.然后是这些字段只是理论上是唯一的,例如使用图书编号为主键,这个图书编号只是理论上来说是唯一的,但实践中可能会出现重复的 情况.所 ...
- java集合 stream 相关用法(1)
java8新增一种流式数据,让操作集合数据更简单方便. 定义基本对象: public class Peo { private String name; private String id; publi ...
- 【APIO2014】Palindromes
#103. [APIO2014]Palindromes 统计 描述 提交 自定义测试 给你一个由小写拉丁字母组成的字符串 ss.我们定义 ss 的一个子串的存在值为这个子串在 ss 中出现的次数乘以这 ...
- 服务器证书日期无效 SSL_DATE_INVALID
一大早上,企业微信中提示不能使用:服务器证书日期无效 错误码:SSL_DATE_INVALID 网上资料也比较少,咨询了一下企业微信客服,建议访问网址:https://www.ssllabs.com ...
- Intellij IDEA Ultimate Edition 14.1 破解
key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7B4EW-U12L4 (2) key:huangwei value:97493-G3A41-0SO24-W57LI-Y2 ...
- 手动爬虫之糗事百科(ptyhon3)
一.调用封装的Url_ProxyHelper类,源码如下 import urllib.request as ur class Url_ProxyHelper: def __init__(self, u ...
- Qt 使用 lambda 表达式做为槽函数时为什么使用 QObject::sender() 获取到的发送信号对象指针为空?
/*! Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; ...
- 数据库字符集(AL32UTF8)和客户端字符集(2%)是不同的
登录oracle数据库时我们会遇到这样的提示信息:“数据库字符集(AL32UTF8)和客户端字符集(2%)是不同的”. 这是由于数据库服务端和客户端的字符集不一致所造成的,服务端字符集和客户端字符集相 ...
- SQL小练习
1.现在有两张表订单表TB_ORDER,包括字段:order_id(订单号),username(用户名),amount(订单金额),order_time(下单时间), product_id(商品ID) ...
- elastic search使用
elastic使用 使用python时注意保持一个好习惯:不要使用类似str.type这样的变量名,很容易引发错误: https://blog.csdn.net/lifelegendc/article ...