[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 ...
随机推荐
- docker-py环境配置
一.系统环境版本介绍: os-version: Linux -.el7.x86_64 python-version: Python six-version: python-six--.el7.noar ...
- boost数据结构any(很有用!)
any是一种特殊的容器,它只能容纳一个元素,但这个元素可以是任意类型; 可以用any保存任何类型,在任何需要的时候取出它; 这种功能和shared_ptr<void>类似,但是 ...
- Angular2 初识
AppComponent 壳的三个实现文件: app.component.ts— 组件的类代码,这是用 TypeScript 写的. app.component.html— 组件的模板,这是用 HTM ...
- 自定义ScrollView 支持添加头部
自定义ScrollView 支持添加头部并且对头部ImageView支持放大缩小,上滑头部缩小,下滑头部显示放大 使用方式: scrollView = (MyScrollView) findViewB ...
- iOS-layoutSubvies和drawRect何时调用
- CNBlog客户端--第一阶段记录
开始 五一小长假由于没有出去玩,所以我就用来继续写我的 CNBlog Android 客户端!首先呢!先上图!让大家看看,我做到哪儿了!! 不知道大家看了是什么感觉哈!有意见请评论哦!! 完成度以及遇 ...
- SPOJ OPTM - Optimal Marks
OPTM - Optimal Marks no tags You are given an undirected graph G(V, E). Each vertex has a mark whic ...
- ClickHouse RPM packages installation from packagecloud.io
Table of Contents Introduction Script-based installation Install script Install packages after scrip ...
- 测试一个服务器的性能,客户要求向数据库(Sqlserver2012)内 1000/s(每插入一千条数据) 的处理能力
通过jmeter很简单就可以完成,可以参考我以前的一篇文章<jmeter创建数据库(Sqlserver2012)测试>. 前提条件:一个数据库:test 数据库下面有一张表:user ...
- angular.js记录
http://www.runoob.com/angularjs/angularjs-tutorial.html 第一部分:快速上手1.1 angularJS四大核心特性1.2 自己动手搭建开发,调试, ...