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 ...
随机推荐
- 一起学习PHP中GD库的使用(二)
在日常的开发过程中,GD 库最常用的功能就是帮我们对图片进行一些处理,当然,除了处理已有的图片之外,它也可以直接来画图,就像我们最常见的图片验证码.今天的内容主要就是和画图有关,所以最后我们也会做一个 ...
- input竖直的输入框,文字从上到下排列
有的时候可能会有这样的需求,一个竖直的输入框,输入信息,文字也是从上到下排列: (但是在移动端或用轮播swiper时不起作用,可以用textarea代替input) <!DOCTYPE html ...
- C# 获取动态类中所有的字段
/// <summary> /// 动态类 获取字典集合 /// </summary> /// <typeparam name= ...
- Mysql实现无插入有更新(不知主键的情况下)
网上很多资料说有两种方式 (必须现有唯一键) 1.INSERT 中ON DUPLICATE KEY UPDATE的使用 2.REPLACE的使用 通过可以得出结果: 如果a和b字段 能决定唯一 例子: ...
- EcShop首页显示特定分类的精品新品热销特价等推荐商品
EcShop首页显示特定分类的精品新品热销特价等推荐商品 很多大型的B2C商城都有特定分类专区,该分类下的[分类名称].[推荐子分类 或 推荐品牌].[大图片/推荐单品].[推荐商品].[促销商品]. ...
- Jmeter系列(13)- 数据库操作之JDBC Connection Configuration配置元件、JDBC Request取样器
Jmeter常见操作数据库场景 准备.制造测试数据 获取.查询测试数据 数据库数据作为参数引用 清理测试环境.删除过程数据 数据库压测 Jmeter操作数据库环境准备 已经安装好的数据库,比如MySq ...
- Python中“if __name__=='__main__':”
在Python当中,如果代码写得规范一些,通常会写上一句"if name=='main:"作为程序的入口,但似乎没有这么一句代码,程序也能正常运行.这句代码多余吗?原理又在哪里? ...
- python编码问题:UnicodeDecodeError: 'gbk' codec can't decode
在获取yaml文件数据时,提示:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 2: illegal multib ...
- 什么鬼?你还搞不懂json和字典的区别??
现在自动化培训烂大街,是个人都能说的上几个框架,面试如果问框架相关问题,求职者只需一瓶 82 年的雪碧,会吹的让你怀疑人生!所以面试官为了更清楚的知道你是停留在表面上的花拳绣腿还是有扎实的基础,就不会 ...
- 跳表--怎么让一个有序链表能够进行"二分"查找?
对于一个有序数组,如果要查找其中的一个数,我们可以使用二分查找(Binary Search)算法,将它的时间复杂度降低为O(logn).那查找一个有序链表,有没有办法将其时间复杂度也降低为O(logn ...