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. Scrapy-redis分布式爬虫爬取豆瓣电影详情页

    平时爬虫一般都使用Scrapy框架,通常都是在一台机器上跑,爬取速度也不能达到预期效果,数据量小,而且很容易就会被封禁IP或者账号,这时候可以使用代理IP或者登录方式爬,然而代理IP很多时候都很鸡肋, ...

  2. SpringBoot-Security-用户权限分配-项目搭建

    SpringBoot原则是约定优于配置,简化spring应用开发,去繁从简,产品级别的应用. SpringBoot有哪些优点1.快速创建独立运行的spring项目与主流框架集成 2.使用嵌入式的ser ...

  3. Oracle 数据库密码过期问题

    (1)在CMD命令窗口中输入:           sqlplus 用户名/密码@数据库本地服务名 as sysdba;(如:sqlplus scott/1234@oracle1 as sysdba; ...

  4. Kubernetes master服务定制编译docker镜像

    前言 之前部署了Kubernetes 1.13.0,发现master服务的启动方式与1.10.4版本有所区别,kube-apiserver.kube-controller-manager和kube-s ...

  5. 求 1 到 n 的所有数的约数和

    求 1 到 n 的所有数的约数和 暴力方法就是枚举每个数,算出他的约数和即可,这样有点慢. 另一种思路,枚举约数,判断他是谁的约数,并记录(即他的倍数有多少个),在乘以他自己. n/i求的是n以内,i ...

  6. Day24&25&26&27:HTML+CSS

    1.网页得三大组成:HTML(标签.皮影的小人) \CSS(布局,皮影的装束) \JS(动作,皮影的操纵者) 2.HTML目录树 3.HTML-标签 成对<>组成,不区分大小写,自闭合标签 ...

  7. 45、gridview在改变位置之后无法完整显示的问题记录

    gridview的父布局为layoutFather,gridview id为 layoutGridview layoutFather   高度设置为130dp layoutGridview高度设置为1 ...

  8. 【Interleaving String】cpp

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...

  9. 【Luogu P1661】扩散

    题目: 一个点每过一个单位时间就会向四个方向扩散一个距离,如图. 两个点$a$.$b$连通,记作$e(a,b)$,当且仅当$a$.$b$的扩散区域有公共部分.连通块的定义是块内的任意两个点$u$.$v ...

  10. CSU-2220 Godsend

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2220 题目 Description Leha somehow found ...