正则表达式匹配-力扣

在这里插入图片描述

考察知识点

python 动态规划 回溯算法

代码

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        memo={}
        def dp(i,j):
            if (i,j) in memo:return memo[(i,j)]
            if j==len(p):return i==len(s)
            if i>len(s):return False
            match=i<len(s) and p[j] in (s[i],".")
            if j <=len(p)-2 and p[j+1]=="*":
                res=dp(i,j+2) or (match and dp(i+1,j))
            else:
                res=match and dp(i+1,j+1)              
            memo[(i,j)]=res
            return res
        
        return dp(0,0) 

参考https://leetcode-cn.com/problems/regular-expression-matching/solution/ji-yu-guan-fang-ti-jie-gen-xiang-xi-de-jiang-jie-b/#comment

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章