leetcode 10/300 正则表达式 py
题目说明
动态路径规划
以s="aab",p="c * a * b"来说明整个的过程
class Solution:
def isMatch(self, s: str, p: str):
if not p: return not s
if not s and len(p) == 1: return False
m = len(s) + 1
n = len(p) + 1
dp = [[False for _ in range(n)] for _ in range(m)]
dp[0][0] = True
# 确定dp数组的第一行,如果遇到了*,只要判断其对应的前面两个元素的dp值
# 注意:我们无需判断p里面的第一个值是否为"*",如果为"*",那肯定匹配不到为Fasle,原数组正好是Fasle,所以直接从2开始判断即可
for j in range(2, n):
if p[j-1] == '*':
dp[0][j] = dp[0][j - 2]
for r in range(1, m):
i = r - 1 # 对应s中的元素
for c in range(1, n):
j = c - 1 # 对应p中的元素
if s[i] == p[j] or p[j] == '.':
dp[r][c] = dp[r - 1][c - 1]
elif p[j] == '*':
if p[j - 1] == s[i] or p[j - 1] == '.':
dp[r][c] = dp[r - 1][c] or dp[r][c - 2]
else:
dp[r][c] = dp[r][c - 2]
else:
dp[r][c] = False
return dp[m - 1][n - 1]
'''
作者:yi-zhi-an-mu-xi
链接:https://leetcode-cn.com/problems/regular-expression-matching/solution/hen-rong-yi-li-jie-de-zheng-ze-biao-da-s-cpgp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
'''
本文来自博客园,作者:xingye_z,转载请注明原文链接:https://www.cnblogs.com/xyzhrrr/p/15480209.html