力扣 10. 正则表达式匹配

动态规划-二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def isMatch(s, p):
def match(i, j):
# 指针未指向字符串s
if i == 0:
return False
# 单个字符匹配任意字符
if p[j - 1] == ".":
return True
# 单个字符一对一匹配
return s[i - 1] == p[j - 1]
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True

for i in range(m + 1):
for j in range(1, n + 1):
# 零到多字符匹配
if p[j - 1] == "*":
dp[i][j] |= dp[i][j - 2] # 匹配零个
if match(i, j - 1): # 要进行匹配
dp[i][j] |= dp[i - 1][j]
else:
# 一对一字符匹配
if match(i, j):
dp[i][j] |= dp[i - 1][j - 1]

return dp[m][n]
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信