Skip to content

Commit d7f2f1f

Browse files
committed
add 10
1 parent 1e91a92 commit d7f2f1f

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ docs/_build/
7474
# PyBuilder
7575
target/
7676
.idea
77+
.gitignore
7778

7879
# Jupyter Notebook
7980
.ipynb_checkpoints

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ LeetCode
3030
| 25 |153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problemset/all/) | c++ | [python](./solution/153FindMinimuminRotatedSortedArray.py) | binary search | M | 2019/04/28 |
3131
| 26 |72|[Edit Distance](https://leetcode.com/problemset/all/) | c++ | [python](./solution/72EditDistance.py) | dp | H | 2019/05/21 |
3232
| 27 |1190|[Reverse Substrings Between Each Pair of Parentheses](https://leetcode.com/problemset/all/) | c++ | [python](./solution/1190ReverseSubstringsBetweenEachPairofParentheses.py) | stack | M | 2019/09/17 |
33+
| 28 |10|[Regular Expression Matching](https://leetcode.com/problemset/all/) | c++ | [python](./solution/10RegularExpressionMatching.py) | 双序列dp | H | 2019/09/18 |
3334

solution/10RegularExpressionMatching.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@ class Solution:
22
def isMatch(self, s: str, p: str) -> bool:
33
m = len(s)
44
n = len(p)
5-
dp = [False *(n+1) for _ in range(m+1)]
5+
dp = [[False] * (n + 1) for _ in range(m + 1)]
66
# dp[i][j] =
7-
# dp[i-1][j-1] 如果 i > 0 , 并且 p[j-1] == '.' or s[i-1] == p[j-1]
8-
# dp[i][j-2] (表示不用 *x ) or ( dp[i-1][j] and (s[i-1] == p[j-1] or p[j-2] == '.') )
9-
# if p[j-1] == '*'
10-
for i in range(m+1):
11-
for j in range(n+1):
12-
dp[][]
7+
# dp[i-1][j-1] 如果 i > 0 , 并且 p[j-1] == '.' or s[i-1] == p[j-1] 或
8+
# (dp[i][j-2] (表示不用 *x ) or ( dp[i-1][j] 在 (s[i-1] == p[j-1] or p[j-2] == '.') )
9+
# if p[j-1] == '*')
10+
for i in range(m + 1):
11+
for j in range(n + 1):
12+
if i == 0 and j == 0:
13+
dp[i][j] = True
14+
continue
15+
if j == 0:
16+
dp[i][j] = False
17+
continue
18+
# j > 0
19+
dp[i][j] = False
20+
if p[j - 1] != '*':
21+
if i > 0 and (p[j - 1] == '.' or s[i - 1] == p[j - 1]):
22+
dp[i][j] = dp[i - 1][j - 1]
23+
else:
24+
if j > 1:
25+
dp[i][j] |= dp[i][j - 2]
26+
if i > 0 and j > 1:
27+
if s[i - 1] == p[j - 2] or p[j - 2] == '.':
28+
dp[i][j] |= dp[i - 1][j]
29+
return dp[m][n]

0 commit comments

Comments
 (0)