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的更多相关文章

  1. 【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 ...

  2. [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 ...

  3. [Leetcode Week9]Word Break

    Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...

  4. 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 ...

  5. 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 ...

  6. 【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 ...

  7. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

  8. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  9. 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 ...

随机推荐

  1. 基于阿里云服务器Linux系统部署JavaWeb项目

    前段时间刚完成一个JavaWeb项目,想着怎么部署到服务器上,边学边做,花了点时间终于成功部署了,这里总结记录一下过程中所遇到的问题及解决方法.之所以选择阿里云,考虑到它是使用用户最多也是最广泛的云服 ...

  2. 小心!FOMO3D的坑

    null 01 前方高能 近日,区块链机构安比(SECBIT)实验室审计后确认,FOMO3D游戏的智能合约存在随机数漏洞可被利用,FOMO3D合约及所有抄袭源码的山寨合约均存在该安全漏洞. 原本设计上 ...

  3. 点击查看大图Activity

    1.使用方式 Intent intent = new Intent(FriendCircleActivity.this, ImageGralleryPagerActivity.class);//0,索 ...

  4. chromium源码阅读--图片处理

    JavaScript 图像替换 JavaScript 图像替换技术检查设备能力,然后“做正确的事”. 您可以通过 window.devicePixelRatio 确定设备像素比,获取屏幕的宽度和高度, ...

  5. lintcode-92-背包问题

    92-背包问题 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 注意事项 你不可以将物品进行切割. 样例 如果有4个物品[2, 3, 5, 7] 如果背包 ...

  6. 在vue-cli创建的项目里配置scss

    第一步,gitbash进入到项目目录 npm install node-sass --save-dev npm install sass-loader --save-dev 第二步:打开webpack ...

  7. mysql yearweek修改开始日期

    MySQL 的yearweek函数默认是从周日~周六,需求需要从周一到周日,看了MySQL的文档后,按照如下使用即可更改开始日期. http://dev.mysql.com/doc/refman/5. ...

  8. iBatis的基本使用

    项目结构: 依赖jar: 数据库依赖: CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, PRIMA ...

  9. oracle 引用类型声明

  10. ES mapping field修改过程

    Elasticsearch 的坑爹事--记录一次mapping field修改过程 http://www.cnblogs.com/Creator/p/3722408.html Elasticsearc ...