[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 ...
随机推荐
- kvm初体验之四:从Host登录Guest的五种方式
1. virt-viewer virt-viewer -c qemu:///system vm1 2. virt-manager (以非root身份运行) virt-manager -c qemu:/ ...
- 【ARIMA】Autoregressive Integrated Moving Average Model
[理论部分] ARIMA包含两部分,自回归AR和移动平均MA: AR:Y(t)-a=b(1){Y(t-1)-a}+u(t) 其中a是y的均值, u(t)是均值为零,恒定方差的不相关随机误差项(噪声 ...
- retrival and clustering : week 3 k-means 笔记
华盛顿大学 machine learning 笔记. K-means algorithm 算法步骤: 0. 初始化几个聚类中心 (cluster centers)μ1,μ2, … , μk 1. 将所 ...
- Android中的<include>标签和<merge>标签
android开发中经常会碰到某一个布局的复用:直接拷贝粘贴并不是是有效的策略,这时候就能够借助<include>标签和<merge>标签来完毕. 官方文档: http://d ...
- Django - 请求与响应、表单、中间件、上下文处理器
请求与响应篇 一.HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象.视图函数的第一个参数(request)是HttpRequest对象在djang ...
- 【BZOJ1367】[Baltic2004]sequence 左偏树
[BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...
- sql优化(2)
转自:https://www.cnblogs.com/Jacck/p/8030455.html 看到一篇非常全面的SQL优化文章,在开发的工作中往往不考虑性能上的缺失(在一开始的时候数据量不大也看不出 ...
- ASP.Net请求处理机制初步探索之旅 - Part 3 管道(转)
开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...
- 8.javascript获取表单中两个数字,并判断大小
获取表单数据的方式: 1.表单注意些id 2.使用document.getElementById("num").value 获取值 3.一定要注意现在得到是string类型,可以用 ...
- 编译型 解释型 C++工作原理
C++教程_w3cschool https://www.w3cschool.cn/cpp/ C++工作原理: C++语言的程序因为要体现高性能,所以都是编译型的.但其开发环境,为了方便测试,将调试环境 ...