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的错误和异常(二)
上回文章中我们讲到了错误是编译和语法运行时会出现的,它们与逻辑无关,是程序员在码代码时不应该出现的,也就是说,这些错误应该是尽量避免带到线上环境的,他们不能通过try...catch捕获到.而异常则正 ...
- php在类中使用回调函数 如array_map
<?php class foo { var $var; function bar() { array_map(array($this, "baz"), ar ...
- disruptor笔记之二:Disruptor类分析
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 安装VM-TOOLS,解压tar包时提示目录磁盘空间不足
在虚拟机里安装了ubuntu-18.04.4-desktop-amd64,安装VM-TOOLS,解压tar包时提示目录磁盘空间不足. 解决方法一: 打开terminal,输入:sudo apt ins ...
- [转载]让你玩转代码的编辑神器phpstorm功能详解
转载网址:http://wwwquan.com/show-66-121-1.html phpstorm包含了webstorm的全部功能,更能够支持php代码.PhpStorm是一个轻量级且便捷的PHP ...
- 鸿蒙内核源码分析(GN应用篇) | GN语法及在鸿蒙的使用 | 百篇博客分析OpenHarmony源码 | v60.01
百篇博客系列篇.本篇为: v60.xx 鸿蒙内核源码分析(gn应用篇) | gn语法及在鸿蒙的使用 | 51.c.h.o 编译构建相关篇为: v50.xx 鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙 ...
- 多个ssh key 配置多个网址
多个 ssh key 配置多个网站 一.生成ssh key ssh-keygen -t rsa -C "你的邮箱" -f ~/.ssh/id_rsa_one ssh-keygen ...
- 深入理解JAVA虚拟机《二》
对象.内存回收和垃圾收集算法 一.引用计数算法(不可靠) 现在很多比较普遍的判断对象是否存活的算法就是引用计数算法,其大概原理是:给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当 ...
- C++ Primer 查漏补缺 —— C++ 中的各种初始化
初学者在刚开始读 C++ Primer 的时候,总是容易被书中各种初始化搞得头大:默认初始化.列表初始化.值初始化.类内初始值.构造函数初始值列表.new int 和 new int() 的区别... ...
- B站视频:CocosCreator Bundle 特性三个实例详解,轻松实现大厅子游戏模式
详细内容:https://forum.cocos.org/t/topic/112146