原题地址:

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统计层数的方法)的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  3. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  4. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

  5. 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 ...

  6. 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 ...

  7. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  8. [LeetCode] 139. Word Break 拆分词句

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  9. 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 ...

随机推荐

  1. PARSEC測试集的应用领域和working set的大小

    參考:tp=&arnumber=4636090">PARSEC vs. SPLASH-2: A Quantitative Comparison of Two Multithre ...

  2. js json ie不支持json

    JSON是包含在JScript 5.8中,而为了向下兼容ie8只有在文档模式是”Internet Explorer 8 Standards”的时候才使用JScripte 5.8,其他时候使用JScri ...

  3. MathType输入补集符号的步骤有哪些

    集合符号在很多的数学领域都会用到,其基本的集合运算可以分为交.并.补这三种.但是一些用户朋友们在编辑文档的时候想输入集合符号这个时候就需要用到数学公式编辑器MathType,但是很多人能够快速地编辑出 ...

  4. VC++ Debug格式化数值显示

    When you watch variables in the Watch or Quick Watch window, the values are displayed using the defa ...

  5. Telnet发送邮件之聊以自慰

    北京的冬天,闲着无聊,得做点什么暖暖脑袋,用windows系统自带工具telnet玩了把邮件发送 准备工作: 1.打开windows系统telnet客户端功能 2.准备两个邮箱帐号(xxx@163.c ...

  6. win10下Import caffe时出现“ImportError: No module named google.protobuf.internal”的解决办法

    解决方法:只要出现和protobuf相关的错误,只要在cmd中输入pip install protobuf,然后等待安装完成即可. ps:这时,可能会出现"pip 不是内部命令"之 ...

  7. ios 时间解析 差8个小时

    啥问题也有:小程序中web开发工具里显示时间正常,ios上显示的查8小时 原因: 使用 NSDate *date = [NSDate date]; 获取的时间是标注的UTC时间,和北京时间相差8小时. ...

  8. iOS 错误提示

    1.CUICatalog: Invalid asset name supplied: , or invalid scale factor: 2.000000 =>原因: You are call ...

  9. 移动端:判断是否微信端、判断手机操作系统(ios或android)

    http://caibaojian.com/browser-ios-or-android.htmlfunction is_weixin() { var ua = window.navigator.us ...

  10. macro-name replacement-text 宏 调试开关可以使用一个宏来实现

    C++ 预处理器_w3cschool https://www.w3cschool.cn/cpp/cpp-preprocessor.html C++ 预处理器 预处理器是一些指令,指示编译器在实际编译之 ...