[Leetcode Week9]Word Break II
Word Break II 题解
题目来源:https://leetcode.com/problems/word-break-ii/description/
Description
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. You may assume the dictionary does not contain duplicate words.
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"].
Solution
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int size = s.size();
string outputStr;
vector<string> result;
vector<bool> flags(size + 1, true);
dfs(s, 0, wordDict, flags, outputStr, result);
return result;
}
void dfs(string& s, int startIdx, vector<string>& wordDict, vector<bool>& flags, string& outputStr, vector<string>& result) {
int size = s.size();
if (startIdx == size) {
result.push_back(outputStr.substr(0, outputStr.size() - 1));
return;
}
for (int i = startIdx; i < size; i++) {
string tempSubStr = s.substr(startIdx, i + 1 - startIdx);
if (find(wordDict.begin(), wordDict.end(), tempSubStr) != wordDict.end() && flags[i + 1]) {
outputStr += tempSubStr + " ";
int preSize = result.size();
dfs(s, i + 1, wordDict, flags, outputStr, result);
if (preSize == result.size())
flags[i + 1] = false;
outputStr.resize(outputStr.size() - tempSubStr.size() - 1);
}
}
}
};
解题描述
这道题虽然表面上是第一道Word Break的升级版,但是不同于第一道,这道题使用动态规划可能会出现问题。刚开始做的时候我就想着改用第一道题的做法就好,但是几次提交都是MLE;修改了代码,减少了变量的使用之后,在后面一个比较长的测例上面一直TLE。自己本机上也是运行那个测例的时候被KILL掉,查看系统日志原因仍然是超内存。查阅了一些资料之后才发现,这道题使用动态规划比较繁琐而且效率不高,没有做好剪枝的话还很容易就会出现MLE。结合各种资料重新修改代码,最终还是采用了DFS来解决,并且采用一个bool
数组flags
来做剪枝标记。
[Leetcode Week9]Word Break II的更多相关文章
- 【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 Week9]Word Break
Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...
- 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 ...
- 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 ...
随机推荐
- (干货分享)mac python+appium环境搭建
因为mac本自带python2.x(不建议卸载,因为本本本身有很多依赖与此),所以装python3的过程极其坎坷,勉强装好后也总是各种报错.这次装appium环境,直接把原来的python3卸了,用h ...
- 在Kotlin编写RecyclerView适配器(KAD 16)
作者:Antonio Leiva 时间:Mar 14, 2017 原文链接:https://antonioleiva.com/recyclerview-adapter-kotlin/ 通过创建Recy ...
- Struts2(七.删除和修改用户功能的实现)
一.删除功能 之前跳转用户列表的时候把用户id,用户名,用户密码存入了ActionContext 1.userlist.jsp <%@ page language="java" ...
- 【Linux运维】Centos7上借助ansible搭建LVS+Keepalived
安装ansible 安装ansible: [root@localhost ~]# /etc/hosts 192.168.19.129 web129.yanglt.com web129 192.168. ...
- 调度器&负载均衡调度算法整理
一.Linux 调度器 Linux中进程调度器已经经过很多次改进了,目前核心调度器是在CFS(Completely Fair Scheduler),从2.6.23开始被作为默认调度器.用作者Ing ...
- 目标检测之Faster-RCNN的pytorch代码详解(数据预处理篇)
首先贴上代码原作者的github:https://github.com/chenyuntc/simple-faster-rcnn-pytorch(非代码作者,博文只解释代码) 今天看完了simple- ...
- QR码与DM码的区别
DM无法表现汉字等其他形式,而QR码能用数据压缩方式来表示汉字,仅用13bit即可表示一个汉字,比其他二维条码表示汉字的效率提高了20%.相较而言,DM码信息容量小,应用简单.而QR在汉字处理上更有优 ...
- nvm版本管理工具安装
windows 安装nvm步骤(shi'yongnvm-windows管理node版本): 瞎几把前言:mac上可以用n来管理node版本,私以为n很好用.家里的win7台式机一直没有安装过任何管理工 ...
- akka与slf4j导致jvm直接crash的诡异
流程很简单,创建actorSystem,通过actorSystem获取AkkaQueryServiceRetriever,进而通过传递path获得的Gateway进行通信. 之前在主项目里跑的很稳定, ...
- 算法(11)Find All Duplicates in an Array
题目:数组的长度是n,里面的数是1到n,其中肯定有重复的,找到里面重复的数字 思路:自己想愣是没有想出来,直接看答案,关键点是看nums[i]和nums[nums[i]-1]之间的关系,遍历整个数组, ...