8000 #10 done · fibers/ex-algorithm@5678180 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5678180

Browse files
committed
#10 done
1 parent b0f145e commit 5678180

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/main/java/com/fibers/algorithm/leetcode/_10/Solution.java

Lines changed: 9 additions & 11 deletions
< 8000 td data-grid-cell-id="diff-e5c723197a28bf0a9dd83866c951866242920dd0855d57c0568081b9ead9e31c-15-15-0" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.fibers.algorithm.leetcode._10;
2+
23
/*
34
* @lc app=leetcode.cn id=10 lang=java
45
*
56
* [10] 正则表达式匹配
67
*/
7-
88
// @lc code=start
99
class Solution {
1010

1111
public static void main(String[] args) {
1212
Solution s = new Solution();
13-
s.isMatch("aa", "a*");
13+
System.out.println(s.isMatch("ab", ".*"));
1414
}
15

1616
public boolean isMatch(String s, String p) {
@@ -24,25 +24,23 @@ public boolean isMatch(String s, String p) {
2424
boolean dp[][] = new boolean[sLen + 1][pLen + 1];
2525
dp[0][0] = true;
2626

27-
if (p.charAt(0) == '.' || p.charAt(0) == s.charAt(0)) {
28-
dp[1][1] = true;
27+
for (int j = 2; j <= pLen; j++) {
28+
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
2929
}
3030

3131
for (int i = 1; i <= sLen; i++) {
3232
for (int j = 1; j <= pLen; j++) {
33-
if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.') {
33+
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
3434
dp[i][j] = dp[i - 1][j - 1];
35-
} else if (p.charAt(j) == '*') {
36-
// abcc* match acb, as c* count as empty
37-
if (p.charAt(j - 1) != s.charAt(i)) {
35+
} else if (p.charAt(j - 1) == '*') {
36+
if (p.charAt(j - 2) == s.charAt(i - 1) || p.charAt(j - 2) == '.') {
37+
dp[i][j] = dp[i - 1][j] || dp[i][j - 1] || dp[i][j - 2];
38+
} else if (p.charAt(j - 2) != s.charAt(i - 1)) {
3839
dp[i][j] = dp[i][j - 2];
39-
} else if (p.charAt(j - 1) == s.charAt(i) || p.charAt(j - 1) == '.') {
40-
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
4140
}
4241
}
4342
}
4443
}
45-
4644
return dp[sLen][pLen];
4745
}
4846
}

0 commit comments

Comments
 (0)
0