word-break-ii leetcode C++
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"].
C++
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict){
vector<string> result;
if(dict.find(s)!=dict.end())
result.push_back(s);
for(int i=1;i<s.size();i++) {
string w = s.substr(i);
if(dict.find(w) == dict.end())
continue;
string str = s.substr(0,i);
vector<string> left = wordBreak(str,dict);
Add(left,w);
result.insert(result.begin(), left.begin(), left.end());
}
return result;
}
void Add(vector<string> &str, string w){
for(vector<string>::iterator it=str.begin();it!=str.end();it++)
*it += " " + w;
}
vector<string> wordBreak2(string s, unordered_set<string> &dict){
vector<string> result;
if (dict.find(s) != dict.end())
result.push_back(s);
for(int i = s.size() -1; i > 0;i--){
string w = s.substr(i);
if(dict.find(w) == dict.end())
continue;
string str = s.substr(0,i);
vector<string> left = wordBreak(str,dict);
Add(left,w);
result.insert(result.end(),left.begin(),left.end());
}
return result;
}
};
word-break-ii leetcode C++的更多相关文章
- 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 ...
- 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 ...
随机推荐
- java版gRPC实战之六:客户端动态获取服务端地址
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 截断误差VS舍入误差
截断误差:是指计算某个算式时没有精确的计算结果,如积分计算,无穷级数计算等,使用极限的形式表达的,显然我们只能截取有限项进行计算,此时必定会有误差存在,这就是截断误差. 舍入误差:是指由于计算机表示 ...
- 洛谷P1060——开心的金明
https://www.luogu.org/problem/show?pid=1060 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈 ...
- UI自动化测试:App的Webview页面元素左滑
一.前言 在做App自动化测试时,我们会遇到如上图所示的列表数据页面左滑删除场景,一般可以通过location.rect方法获取对应列表的元素坐标,然后使用TouchAction或者swipe滑动 ...
- mysql 不常用的存储引擎
csv 数据文件可以编辑;每一列不能为空,不支持索引:文件保存数据,cat可以查看数据;用处:数据交换中间表--excel表导入数据等; Archive 对表数据进行压缩,磁盘i/o减少:节省空间;只 ...
- 接口测试checklist
静态测试 接口文档与设计文档对应 接口定义 接口定义与数据库定义 业务功能测试 系统全流程验证 逆向全流程验证 事务性测试 边界值测试 业务规则边界值 场景分析合理长度 场景分析合理数据量 输入.输出 ...
- LR11可打开网页,但录制为空
环境:WIN7+LR11+360安全浏览器9.0 LR11可打开网页,但录制为空解决方案:(3步) 1. tools-Recording Options -->Network-->Port ...
- 定要过python二级选择题二套
1. 2. 3. 4. 5. 6. 7. 8. 非主属性???? 9. 10.. 11.` 调用只是调用,但是没有返回值,,应为里面没有输出 12. 13. 14. 15. 16. 18. 19.
- 鸿蒙内核源码分析(内存管理篇) | 虚拟内存全景图是怎样的 | 百篇博客分析OpenHarmony源码 | v12.04
百篇博客系列篇.本篇为: v12.xx 鸿蒙内核源码分析(内存管理篇) | 虚拟内存全景图是怎样的 | 51.c.h .o 内存管理相关篇为: v11.xx 鸿蒙内核源码分析(内存分配篇) | 内存有 ...
- Nresource服务之接口缓存化
1. 背景 Nresource服务日均4.5亿流量,考虑到未来流量急增场景,我们打算对大流量接口进行缓存化处理:根据服务管理平台数据统计显示getUsableResoureCount接口调用量很大,接 ...