力扣算法——140WordBreakII【H】
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input:
s = "catsanddog
"
wordDict =["cat", "cats", "and", "sand", "dog"]
Output:
[
"cats and dog",
"cat sand dog"
]
Example 2:
Input:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
"pine apple pen apple",
"pineapple pen apple",
"pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[] Solution:
方法一,使用递归:【通不过牛客的例子】
class Solution {
public:
unordered_map<string, vector<string>>map;
vector<string> wordBreak(string s, unordered_set<string> &dict) {
if (map.find(s) != map.end())return map[s];
if (s.empty())return { "" };
vector<string>res;
for (auto word : dict)
{
if (s.substr(, word.size()) != word)continue;
vector<string>rem = wordBreak(s.substr(word.size()), dict);
for (auto str : rem)
res.push_back(word + (str.empty() ? "" : " ") + str);
}
return map[s] = res;
}
};
方法二:使用动态规划
//使用动态规划 class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
int len = s.length();
dp = new vector<bool>[len];
for (int pos = ; pos < len; pos++) {
for (int i = ; i < len - pos + ; i++) {
if (dict.find(s.substr(pos, i)) != dict.end())
dp[pos].push_back(true);
else
dp[pos].push_back(false);
}
}
dfs(s, len - );
return res;
}
void dfs(string s, int n) {
if (n >= ) {
for (int i = n; i >= ; i--) {
if (dp[i][n - i]) {
mid.push_back(s.substr(i, n - i + ));
dfs(s, i - );
mid.pop_back();
}
}
}
else {
string r;
for (int j = mid.size() - ; j >= ; j--) {
r += mid[j];
if (j > )
r += " ";
}
res.push_back(r);
}
}
vector<bool> *dp;
vector<string> res;
vector<string> mid;
};
力扣算法——140WordBreakII【H】的更多相关文章
- 力扣算法——135Candy【H】
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中,评分高 ...
- 力扣算法题—069x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- 力扣算法经典第一题——两数之和(Java两种方式实现)
一.题目 难度:简单 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数, 并返回它们的数组下标. 你可以假设每种输入只会对应一 ...
- C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法
题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...
- 力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...
- 力扣算法题—052N皇后问题2
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...
- 力扣算法题—051N皇后问题
#include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...
随机推荐
- sizeof 运算结果与编译系统有关
研究与实现相关的layout没多大意义 参考:有关c++中类的虚拟继承sizeof问题 情况1:<剑指offer>纪念版题,sizoef(空类)的结果? class A{}; sizeof ...
- 怎么追加byte内容
public byte[] InsertByte(string dx) { List<byte> temp = new List<byte>(); byte[] b= Enco ...
- JavaScript Set
function Set() { var items = {}; this.has = function(value) { return value in items } this.add = fun ...
- leetcode.排序.451根据字符出现频率排序-Java
1. 具体题目 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r ...
- 编译Linux-2.6.23内核中遇见的错误
编译linux-2.6.23 错误[1]: elf_x86_64: 没有那个文件或目录 原因是 gcc 4.6 不再支持 linker-style 架构.在 arch/x86/vdso/Makefil ...
- MVC http://stackoverflow.com/tags/model-view-controller/info
About model-view-controller Model–View–Controller (MVC) is an architectural pattern used in software ...
- css的9个常用选择器
1.类选择器(通过类名进行选择) <!DOCTYPE html> <html> <head> <title></title> </he ...
- JS 富文本编码、解码
第一种 escape()和unescape()方法 escape() 方法能够把 ASCII之外的所有字符转换为 %xx 或 %uxxxx(x表示十六进制的数字)的转义序列.从 \u000 到 \u0 ...
- ubuntu服务器端使用无界面selenium+ chrome + headless
本来想直接用Ubuntu系统里面的firefox来实现selenium自动操作签到的,但是总是出各种问题.没办法,改为Chrome.参考:Ubuntu 线上无界面服务器 使用selenium chro ...
- day02 html body中的标签
day02 html 一.body中的标签 a标签: <!DOCTYPE html> <html lang="en"> <head> ...