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 ...
随机推荐
- UVA_10653 公主与王子 #刘汝佳DP题刷完计划
题意如蓝书66页例题27所示. 这个问题描述了一个LCS的特殊情况——单个字符串内所有元素各不相同. 题目要求输入两个数字串,A,B,要求求出最长公共字串.且数字上限是256*256. 做法:数组A表 ...
- This application has request the Runtime to terminate it in an unusual way.
Q: CertsMV.exe gui popup two dialogs as follow. A: 测试发现是分配内存导致,频繁分配内存(大约6M) 可能是堆管理导致 分配大内存分配失败,程序未对 ...
- 46、android studio第一次使用时卡在gradle下载怎么解决?
如果没法FQ或者FQ后网速慢,哥教你一个快速解决方案. 在根目录下的.gradle目录下,找到wrapper/dists目录,如果当前正在下载gradle.x.xx-all.zip,那么会发现grad ...
- Python-S9——Day110-Git继续
1 当日内容概要 2 内容回顾 3 Git版本控制之多人协同开发 4 Git版本控制之fork 5 版本控制之其他 6 Redis之字典基本操作 7 Django中操作Redis 8 Django缓存 ...
- python-os模块及md5加密
常用内置方法 __doc__打印注释 __package__打印所在包 __cached__打印字节码 __name__当前为主模块是__name__ == __main__ __file__打印文件 ...
- 使用 htaccess 重写 url,隐藏查询字符串
例如我们有如下 URL: http://example.com/users.php?name=tania 但是我们想要让 URL 变成如下: http://example.com/users/tani ...
- 【Appnium+C#+Winform自动化测试系列】一、获取本机连接的设备、启动多个Appnium和获取本机启动的Appnium
本系列内容,准备根据所完成的项目为基线,一步一步的把整个设计和实现过程梳理. 先从基本的一些环境问题入手,梳理清楚关于手机设备和Appnium.因为我们在后面的建立Appnium连接时,需要设备名字和 ...
- django ORM 外键详解
Django中的外键: 首先,为了方便理解,我们把使用ForeignKey的字段所在的表定义为从表,把ForeignKey中to参数连接的表称为主表. 外键使用的先决条件: 在mysql数据表中,数据 ...
- python技巧:拆分多层嵌套列表
方法一: >>> import itertools >>> a = [[1, 2], [3, 4], [5, 6]] >>> list(itert ...
- Spring整合Hibernate与Struts
整合S2SH 一.导入jar包 Spring jar包 Hibernate jar包 Struts2 jar包 以上就是整合需要的所有jar包,当然其中有重复的包,(对比之后去掉版本低的就可以了,还有 ...