From 1dc14cd494eac0fc74364b71c1d60d4e8109e62c Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Wed, 4 Mar 2020 22:10:46 +0800 Subject: [PATCH] 140. Word Break II :beer: --- README.md | 1 + solutions/140. Word Break II.md | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 solutions/140. Word Break II.md diff --git a/README.md b/README.md index 4cf622d..5e2b605 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 | 137 |[Single Number II](https://leetcode.com/problems/single-number-ii/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/137.%20Single%20Number%20II.md)|Medium| | | 138 |[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/138.%20Copy%20List%20with%20Random%20Pointer.md)|Medium| | | 139 |[Word Break](https://leetcode.com/problems/word-break/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/139.%20Word%20Break.md)|Medium| | +| 140 |[Word Break II](https://leetcode.com/problems/word-break-ii/)|[C++](solutions/140.%20Word%20Break%20II.md)|Hard| | | 141 |[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/141.%20Linked%20List%20Cycle.md)|Easy| | | 142 |[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/142.%20Linked%20List%20Cycle%20II.md)|Medium| | | 143 |[Reorder List](https://leetcode.com/problems/reorder-list/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/143.%20Reorder%20List.md)|Medium| | diff --git a/solutions/140. Word Break II.md b/solutions/140. Word Break II.md new file mode 100644 index 0000000..f8ed86f --- /dev/null +++ b/solutions/140. Word Break II.md @@ -0,0 +1,41 @@ +# [140. Word Break II](https://leetcode.com/problems/word-break-ii/) + +# 思路 + +这题是139. Word Break的升级版,139题只让我们判断给定的字符串s是否可以拆分成字典中的词,而这题让我们求出所有可以拆分成的情况。139题我们用的是动态规划或者带记忆的递归,此题其实也是类似的。我们用一个cache来记录已经求过的结果,即`cache[s]`就等于s被拆分的所有情况,那么我们就可根据`cache[s去掉某个前缀后的子串]`的结果求得`cache[s]`,伪代码如下: + +``` +cache[s] = {} +for word in wordDict: + if word是s的前缀: + sub_res = s去掉前缀word后的拆分结果(即递归调用) + for ss in subres: + cache[s].push(word + " " + ss) +``` + +# C++ +``` C++ +class Solution { +private: + unordered_map>cache; + vector helper(string s, const vector& wordDict){ + if(s.empty()) return {""}; // 注意不是空数组, 因为如果为空就代表了无法拆分 + if(cache.count(s)) return cache[s]; + + vectorres; + for(string word: wordDict){ + if(s.substr(0, word.size()) != word) continue; + vectorsubres = helper(s.substr(word.size()), wordDict); + for(string &ss: subres) + res.push_back(word + (ss.empty() ? "" : " ") + ss); + } + // 如果不能被拆分res将为空数组 + cache[s] = res; + return res; + } +public: + vector wordBreak(string s, vector& wordDict) { + return helper(s, wordDict); + } +}; +```