From c5fba7530bbc3848f123b2d846f8cd4ef1490213 Mon Sep 17 00:00:00 2001 From: zongyanqi Date: Sun, 3 Dec 2017 22:22:05 +0800 Subject: [PATCH] add solution comments --- 010-Regular-Expression-Matching.js | 29 ++++++++++++++++--- 051-N-Queens.js | 2 +- ...718-Maximum-Length-of-Repeated-Subarray.js | 11 +++++-- 3 files changed, 35 insertions(+), 7 deletions(-) rename "718-Maximum-Length-of\342\200\223Repeated-Subarray.js" => 718-Maximum-Length-of-Repeated-Subarray.js (82%) diff --git a/010-Regular-Expression-Matching.js b/010-Regular-Expression-Matching.js index 142a980..4dd6370 100644 --- a/010-Regular-Expression-Matching.js +++ b/010-Regular-Expression-Matching.js @@ -22,6 +22,14 @@ */ /** + * + * 解题思路 + * + * 动态规划 + * 注意体感要求, isMatch 为全匹配 + * 例子中的 isMatch("aab", "c*a*b") -> true + * 是因为匹配 0个c, 2个a, 一个b + * * @param {string} s * @param {string} p * @return {boolean} @@ -40,7 +48,12 @@ var isMatch = function (s, p) { for (var i = 0; i <= m; i++) { for (var j = 1; j <= n; j++) { if (p[j - 1] === '*') { - dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] === p[j - 2] || p[j - 2] === '.') && dp[i - 1][j]); + // isMatch('a', 'a.*') + // 如果j-1是*, 那么j-2可以出现0次; + // 所以可以直接看 dp[i][j-2] + dp[i][j] = dp[i][j - 2] || + // isMatch('a', 'aa*') + (i > 0 && (s[i - 1] === p[j - 2] || p[j - 2] === '.') && dp[i - 1][j]); } else { dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] === p[j - 1] || p[j - 1] === '.'); @@ -48,9 +61,17 @@ var isMatch = function (s, p) { } } - // console.log(dp); + console.log(dp.map(a => a.map(a => a ? 1 : 0))); return dp[m][n]; }; -console.log(isMatch("aab", "c*a*b")); -console.log(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s")); +// console.log(isMatch("aa", "a"), '→', false) +// console.log(isMatch("aa", "aa"), '→', true) +// console.log(isMatch("aaa", "aa"), '→', false) +// console.log(isMatch("aa", "a*"), '→', true) +// console.log(isMatch("aa", ".*"), '→', true) +// console.log(isMatch("ab", ".*"), '→', true) +// console.log(isMatch("aab", "c*a*b"), '→', true) + +console.log(isMatch("a", "a.*")); +// console.log(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s")); diff --git a/051-N-Queens.js b/051-N-Queens.js index c7f424b..a4bc19c 100644 --- a/051-N-Queens.js +++ b/051-N-Queens.js @@ -1,7 +1,7 @@ /** * https://leetcode.com/problems/n-queens/description/ * Difficulty:Hard - * + * * The n-queens puzzle is the problem of placing n queens on an n×n chessboard * such that no two queens attack each other. * diff --git "a/718-Maximum-Length-of\342\200\223Repeated-Subarray.js" b/718-Maximum-Length-of-Repeated-Subarray.js similarity index 82% rename from "718-Maximum-Length-of\342\200\223Repeated-Subarray.js" rename to 718-Maximum-Length-of-Repeated-Subarray.js index f0bb4e5..a2c5154 100644 --- "a/718-Maximum-Length-of\342\200\223Repeated-Subarray.js" +++ b/718-Maximum-Length-of-Repeated-Subarray.js @@ -6,8 +6,8 @@ * * Example 1: * Input: - * A: [1,2,3,2,1] - * B: + * A: [1, 2, 3, 2, 1] + * B: [3, 2, 1, 4, 7, 8] * Output: 3 * Explanation: * The repeated subarray with maximum length is [3, 2, 1]. @@ -19,6 +19,13 @@ */ /** + * 解题思路 + * + * 动态规划 + * + * dp[i][j] 以 A[i-1] B[j-1] 结尾的最长子串长度 + * dp[i][j] = A[i - 1] === B[j - 1] ? dp[i - 1][j - 1] + 1 : 0; + * * @param {number[]} A * @param {number[]} B * @return {number}