Word Break II -- leetcode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].
A solution is ["cats and dog", "cat sand dog"].
基本思路:
深度递归(暴力偿试法)
再结合剪枝操作。
剪枝操作思路为:
使用一个数组:breakable[i], 表示从第 i个字符向后直至结束。是否可分解为句子。
初始皆为true.
当发现从一个位置不可分隔成数组。则置上标志,以避免兴许的反复偿试。
推断不可分隔也非常easy,即从一个位置起開始递归后。假设结果集没有添加,则该位置不可分隔。
此代码在leetcode上实际运行时间为4ms。
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<string> ans;
vector<int> breakable(s.size()+1, true);
dfs(ans, s, wordDict, "", 0, breakable);
return ans;
}
void dfs(vector<string> &ans, const string &s, const unordered_set<string> &wordDict,
string sentence, int start, vector<int> &breakable) {
if (start == s.size()) {
ans.push_back(sentence);
return;
}
if (!sentence.empty())
sentence.push_back(' ');
const int old_size = ans.size();
for (int i=start+1; i<=s.size(); i++) {
if (!breakable[i])
continue;
const string word(s.substr(start, i-start));
if (wordDict.find(word) != wordDict.end()) {
dfs(ans, s, wordDict, sentence + word, i, breakable);
}
}
if (old_size == ans.size())
breakable[start] = false;
}
};
Word Break II -- leetcode的更多相关文章
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Word Break II——LeetCode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【LeetCode】140. Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
随机推荐
- vsphere client 参数
转自:http://blog.163.com/sword_111/blog/static/66589416201422964544918/ C:\Program Files (x86)\VMware\ ...
- ASP.NET Core 2.2 十九. 你扔过来个json,我怎么接
原文:ASP.NET Core 2.2 十九. 你扔过来个json,我怎么接 前文说道了Action的激活,这里有个关键的操作就是Action参数的映射与模型绑定,这里即涉及到简单的string.in ...
- GO语言学习(十三)Go 语言变量作用域
Go 语言变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. Go 语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函 ...
- 洛谷 P1551 亲戚
洛谷 P1551 亲戚 题目背景 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 题目描 ...
- js进阶 14-4 $.get()方法和$.post()方法如何使用
js进阶 14-4 $.get()方法和$.post()方法如何使用 一.总结 一句话总结:$.get(URL,callback); $.post(URL,data,callback); callba ...
- php字符串函数分类总结
php字符串函数分类总结 一.总结 explode str_split str_word_count strtolower 二.php字符串函数分类总结 php内置了98个字符串函数(除了基于正 ...
- android studio 一次编译错误:Error:Minimum supported Gradle version is 2.14.1.
因为需要,今天从git上重新下载工程到另一个目录下,结果运行的时候报了这个错:Error:Minimum supported Gradle version is 2.14.1. Current ve ...
- Android 获取联系人手机号码、姓名、地址、公司、邮箱、生日
public void testGetAllContact() throws Throwable { //获取联系人信息的Uri Uri uri = ContactsContract.Contacts ...
- regexp模式匹配+location页面跳转+cookie/localstorage本地存储
学习js的过程中,根据知识点编写一些code进行测试,以便检验. 这段程序使用了以下知识点: 1.regexp,对数据进行模式匹配 2.使用location对象进行页面跳转. 3.cookie/loc ...
- poj 3071 Football(线段树+概率)
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2801 Accepted: 1428 Descript ...