LeetCode OJ--Word Break II ***@
https://oj.leetcode.com/problems/word-break-ii/
class Solution {
public:
unordered_set<string> dict;
string s;
unordered_map<string, vector<string> > memory; //用来记录一个string可以的组成
vector<string> wordBreak(string s, unordered_set<string> &dict) {
memory.clear();
this->dict = dict;
this->s = s;
return subWordBreak(s);
}
vector<string> subWordBreak(string &s)
{
//先看是否之前处理过这个 子串
if(memory.find(s) != memory.end())
return memory[s];
// 新建一个 memory中的项,即返回值
vector<string> result;
if(s.size() <= )
return result;
// 广度,然后递归
// 个数
for(int i = ; i <= s.size(); i++)
{
// 找出所有合理的小片段,再递归剩下的片段
// if is a valid, belongs to dict
string subS = s.substr(,i);
if(dict.find(subS) != dict.end())
{
if(i == s.size())
result.push_back(subS);
else
{
// 递归调用 剩下的部分
string leftstr = s.substr(i,s.size() - i);
vector<string> tmp = subWordBreak(leftstr);
for(int j = ; j < tmp.size(); j++)
{
string item = subS;
item.append(" ");
item.append(tmp[j]);
result.push_back(item);
}
}
}
}
memory.insert(make_pair(s,result));
return result;
}
};
LeetCode OJ--Word Break II ***@的更多相关文章
- [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 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- leetcode 140. Word Break II ----- java
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode OJ——Word Break
http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...
- Java for LeetCode 140 Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 【leetcode】Word Break II (hard)★
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Leetcode#140 Word Break II
原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 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 ...
随机推荐
- Xadmin后台管理系统搭建基于Django1.11.11+Python3.6
安装python及Django百度即可 主要介绍Xadmin安装 访问地址:https://github.com/sshwsfc/xadmin 下载 安装好之后,将xamdin目录复制到项目 我放在 ...
- uva1422 二分法+优先队列贪心
题意:有n个任务,每个任务必须在在时刻[r, d]之内执行w的工作量(三个变量都是整数).处理器执行的速度可以变化,当速度为s时,一个工作量为w的任务需要 执行的时间为w/s个单位时间.另外不一定要连 ...
- [NOIP2012]疫情控制(二分答案+倍增+贪心)
Description H国有n个城市,这n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境 ...
- Redis实现之对象(二)
列表对象 列表对象的编码可以是ziplist或者linkedlist,ziplist编码的列表对象使用压缩列表作为底层实现,每个压缩列表节点(entry)保存了一个列表元素.举个栗子,如果我们执行RP ...
- Ping过程&ICMP
1.ICMP(Internet控制消息协议) ICMP=Internet Control Message Protocol 它是TCP/IP协议族的一个子协议 作用:用于在IP主机.路由之间传递控制消 ...
- Docker danriti/nginx-gunicorn-flask 使用
现成的镜像,已经配置好nginx-gunicorn-flask,可直接部署flask 项目 直接部署flask项目 安装镜像 如果默认源比较慢,可以换成163镜像源 http://hub-mirror ...
- LeetCode668马在棋盘上的概率
已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有一个 “马”(也译作 “骑士”)位于 (r, c) ...
- PAT——乙级1016
乙级PAT的1016 乙级的题相对比较简单,我也是主要联系写代码的格式,而不是联系算法. 1016 部分A+B (15 point(s)) 正整数 A 的“DA(为 1 位整数)部分”定义为由 ...
- Unable to execute dex: Multiple dex files define 问题
今天在run公司的android project时候,报这个错误. 1. Clean Project, 重启Eclipse 没有解决. 2. 看到别人遇到的相同错误,解决方法如下: http://bl ...
- 【转】unity自带寻路Navmesh入门教程(一)
http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/ 说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后 ...