Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f93e55d

Browse files
author
zongyanqi
committed
add solution comments
1 parent c7228e3 commit f93e55d

3 files changed

+35
-7
lines changed

010-Regular-Expression-Matching.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
*/
2323

2424
/**
25+
*
26+
* 解题思路
27+
*
28+
* 动态规划
29+
* 注意体感要求, isMatch 为全匹配
30+
* 例子中的 isMatch("aab", "c*a*b") -> true
31+
* 是因为匹配 0个c, 2个a, 一个b
32+
*
2533
* @param {string} s
2634
* @param {string} p
2735
* @return {boolean}
@@ -40,17 +48,30 @@ var isMatch = function (s, p) {
4048
for (var i = 0; i <= m; i++) {
4149
for (var j = 1; j <= n; j++) {
4250
if (p[j - 1] === '*') {
43-
dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] === p[j - 2] || p[j - 2] === '.') && dp[i - 1][j]);
51+
// isMatch('a', 'a.*')
52+
// 如果j-1是*, 那么j-2可以出现0次;
53+
// 所以可以直接看 dp[i][j-2]
54+
dp[i][j] = dp[i][j - 2] ||
55+
// isMatch('aa', 'aa*')
56+
(i > 0 && (s[i - 1] === p[j - 2] || p[j - 2] === '.') && dp[i - 1][j]);
4457
} else {
4558
dp[i][j] = i > 0 && dp[i - 1][j - 1] &&
4659
(s[i - 1] === p[j - 1] || p[j - 1] === '.');
4760
}
4861
}
4962
}
5063

51-
// console.log(dp);
64+
console.log(dp.map(a => a.map(a => a ? 1 : 0)));
5265
return dp[m][n];
5366
};
5467

55-
console.log(isMatch("aab", "c*a*b"));
56-
console.log(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s"));
68+
// console.log(isMatch("aa", "a"), '→', false)
69+
// console.log(isMatch("aa", "aa"), '→', true)
70+
// console.log(isMatch("aaa", "aa"), '→', false)
71+
// console.log(isMatch("aa", "a*"), '→', true)
72+
// console.log(isMatch("aa", ".*"), '→', true)
73+
// console.log(isMatch("ab", ".*"), '→', true)
74+
// console.log(isMatch("aab", "c*a*b"), '→', true)
75+
76+
console.log(isMatch("a", "a.*"));
77+
// console.log(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s"));

051-N-Queens.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* https://leetcode.com/problems/n-queens/description/
33
* Difficulty:Hard
4-
*
4+
*
55
* The n-queens puzzle is the problem of placing n queens on an n×n chessboard
66
* such that no two queens attack each other.
77
*

718-Maximum-Length-of–Repeated-Subarray.js renamed to 718-Maximum-Length-of-Repeated-Subarray.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*
77
* Example 1:
88
* Input:
9-
* A: [1,2,3,2,1]
10-
* B:
9+
* A: [1, 2, 3, 2, 1]
10+
* B: [3, 2, 1, 4, 7, 8]
1111
* Output: 3
1212
* Explanation:
1313
* The repeated subarray with maximum length is [3, 2, 1].
@@ -19,6 +19,13 @@
1919
*/
2020

2121
/**
22+
* 解题思路
23+
*
24+
* 动态规划
25+
*
26+
* dp[i][j] 以 A[i-1] B[j-1] 结尾的最长子串长度
27+
* dp[i][j] = A[i - 1] === B[j - 1] ? dp[i - 1][j - 1] + 1 : 0;
28+
*
2229
* @param {number[]} A
2330
* @param {number[]} B
2431
* @return {number}

0 commit comments

Comments
 (0)