From 8092f49a57721d87542de7abba09dd7fceff00b8 Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Tue, 27 Dec 2022 22:52:05 +0800 Subject: [PATCH] add 97 --- README.md | 1 + solutions/97. Interleaving String.md | 47 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 solutions/97. Interleaving String.md diff --git a/README.md b/README.md index cc1493f..743e2c0 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 | 94 |[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/94.%20Binary%20Tree%20Inorder%20Traversal.md)|Medium| | | 95 |[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/95.%20Unique%20Binary%20Search%20Trees%20II.md)|Medium| | | 96 |[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/96.%20Unique%20Binary%20Search%20Trees.md)|Medium| | +| 97 |[Interleaving String](https://leetcode.cn/problems/interleaving-string/)|[C++](solutions/97.%20Interleaving%20String.md)|Medium| | | 98 |[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/98.%20Validate%20Binary%20Search%20Tree.md)|Medium| | | 100 |[Same Tree](https://leetcode.com/problems/same-tree)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/100.%20Same%20Tree.md)|Easy| | | 101 |[Symmetric Tree](https://leetcode.com/problems/symmetric-tree)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/101.%20Symmetric%20Tree.md)|Easy| | diff --git a/solutions/97. Interleaving String.md b/solutions/97. Interleaving String.md new file mode 100644 index 0000000..84d7392 --- /dev/null +++ b/solutions/97. Interleaving String.md @@ -0,0 +1,47 @@ +# [97. Interleaving String](https://leetcode.cn/problems/interleaving-string/) + +# 思路 +仔细分析问题不难发现会涉及到大量子问题,所以我们考虑用动态规划求解。我们定义 dp[i][j] 表示 s1 的前 i 个元素和 s2 的前 j 个元素是否能交错组成 s3 的前 i + j 个元素。可得到初始状态和状态转移方程为: +``` +初始态(即都为空串):dp[0][0] = true +转移方程:dp[i][j] = (dp[i-1][j] && s3[i+j-1] == s1[i-1]) || (dp[i][j-1] && s3[i+j-1] == s2[j-1]) +``` +再考虑下边界条件就不难写出代码。另外由于 dp[i][j] 只和左上元素有关,故可以考虑使用滚动数组优化空间复杂度为O(s2.size())。 + + + +# C++ +``` C++ +class Solution { +public: + bool isInterleave(string s1, string s2, string s3) { + if(s1.size() + s2.size() != s3.size()) return false; + int n1 = s1.size(), n2 = s2.size(), n3 = s3.size(); + if(n1 == 0) return s2 == s3; + if(n2 == 0) return s1 == s3; + + // vector>dp(n1 + 1, vector(n2 + 1, false)); + // for(int i = 0; i <= n1; i++){ + // for(int j = 0; j <= n2; j++){ + // if(i == 0 && j == 0) dp[i][j] = true; + // else if(i > 0 && dp[i-1][j] && s3[i+j-1] == s1[i-1]) dp[i][j] = true; + // else if(j > 0 && dp[i][j-1] && s3[i+j-1] == s2[j-1]) dp[i][j] = true; + // // else dp[i][j] = false; + // } + // } + // return dp[n1][n2]; + + // 滚动数组空间优化 + vectordp(n2 + 1, false); + for(int i = 0; i <= n1; i++){ + for(int j = 0; j <= n2; j++){ + if(i == 0 && j == 0) dp[j] = true; + else if(i > 0 && dp[j] && s3[i+j-1] == s1[i-1]) dp[j] = true; + else if(j > 0 && dp[j-1] && s3[i+j-1] == s2[j-1]) dp[j] = true; + else dp[j] = false; // 注意这里需重置成false, 因为dp[i-1][j]可能等于true + } + } + return dp[n2]; + } +}; +``` \ No newline at end of file