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 ***@的更多相关文章

  1. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  2. 【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 ...

  3. [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 ...

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

  5. LeetCode OJ——Word Break

    http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...

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

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

  8. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

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

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

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

随机推荐

  1. JAVA解析XML有哪几种方法?并简述各自的优缺点

    DOM: 是用与平台和语言无关的方式表示XML文档的官方W3C标准,分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.是基于信息层次的 优点有:由于树在内存中是持久的,因此可以修改它以 ...

  2. [GDOI2016][树链剖分+主席树]疯狂动物城

    题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...

  3. HDU 5657 CA Loves Math 状压DP + 枚举

    题意: 给出\(A(2 \leq A \leq 11), n(0 \leq n \leq 10^9), k(1 \leq k \leq 10^9)\). 求区间\([1, A^n]\)中各个数字互不相 ...

  4. easyui datagrid复选框控制单选

    使用easyui datagrid的时候,由于对数据表格操作太多,并且有单选和多选功能因此采用复选框.但是在单选的状态,使用CheckOnSelect和singleselect时发现,页面有明显延迟, ...

  5. 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站

    现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...

  6. [网站公告]11月26日00:00-04:00阿里云RDS升级

    大家好,11月26号00:00-04:00(今天夜里),阿里云将对我们所用的SQL Server RDS实例所在的物理主机做升级操作(目前博客园整站运行于阿里云上),升级期间RDS实例会有2次闪断,每 ...

  7. 【Swap Nodes in Pairs】cpp

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  8. git+jenkins持续集成三-定时构建语法

    构建位置:选择或创建工程_设置_构建触发器 1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月 ...

  9. [转] linux中 参数命令 -- 和 - 的区别

    在 Linux 的 shell 中,我们把 - 和 -- 加上一个字符(字符串)叫做命令行参数. 主流的有下面几种风格Unix 风格参数 前面加单破折线 -BSD 风格参数 前面不加破折线GNU 风格 ...

  10. js判断时间是否过期

    var myDate=new Date(); myDate.setFullYear(2014,2,1); //2014年3月1日 //注意:表示月份的参数介于 0 到 11 之间.也就是说,如果希望把 ...