Leetcode(10)正则表达式匹配

Leetcode(10)正则表达式匹配

[题目表述]:

给定一个字符串 (s) 和一个字符模式 (p)。实现支持 '.' 和 '*' 的正则表达式匹配。

'.' 匹配任意单个字符。
'*' 匹配零个或多个前面的元素。
匹配应该覆盖整个字符串 (s) ,而不是部分字符串。

第一次:未完成(未能解决.*问题

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        index_s=0
        index_p=0
        form_num=''
        if len(s)==0:
            if len(p)==0 or p==".*":   return True
            else: return False
        if len(p)==0:
            return False
            
        for i in p: ##s p都不为空
            if  index_s==len(s): return False
            elif i==s[index_s] or i=='.':
                form_num=p[index_p]
                index_s+=1
                index_p+=1
            elif i=='*':
                if index_p==0:
                    form_num=p[index_p]
                    index_p+=1
                elif form_num=='.':
                    ##if index_p!=len(s):
                    ##    没有很好的办法去处理.*后面还有字符的问题
                    ##else: return True


                elif form_num==s[index_s]:
                    while form_num==s[index_s]:
                        index_s+=1
                        if index_s==len(s): return True
                    form_num=p[index_p]
                    index_p+=1
                else:
                    form_num=p[index_p]
                    index_p+=1
            elif p[index_p+1]=='*':
                    form_num=p[index_p]
                    index_p+=1
            else: return False
        if  index_s!=len(s):  return False
        else: return True

第二种方法:二维List动态规划dp

执行用时:80 ms ; 内存消耗:13MB 效果:很好

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        #if not s or not p:
            #return False
        s_len = len(s)
        p_len = len(p)
        dp = [[False] * (p_len + 1) for _ in range(s_len + 1)]
        #print(dp)
        dp[0][0] = True
        for i in range(p_len):
            if p[i] == "*" and dp[0][i - 1]:
                dp[0][i + 1] = True
        #print(dp)
        for i in range(s_len):
            for j in range(p_len):
                if p[j] == s[i] or p[j] == ".":
                    dp[i + 1][j + 1] = dp[i][j]
                elif p[j] == "*":
                    if p[j - 1] != s[i]:
                        dp[i + 1][j + 1] = dp[i + 1][j - 1]
                    if p[j-1] == s[i] or p[j-1] == ".":
                        dp[i+1][j+1] = (dp[i][j+1] or dp[i+1][j]   or  dp[i+1][j-1])
        #print(dp)
        return dp[-1][-1]

学习

  • 正则表达式的解法不是从前往后解,而是从后往前解,所以复杂点应该是用回溯法,而非第一次的从前遍历
  • 待解

第三次:递归

执行用时:1880 ms;内存消耗:11.8MB; 效果:不咋样

class Solution:
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        in_str = s
        pt = p


        if not pt:
            return not in_str


        first_match = bool(in_str) and pt[0] in {in_str[0], '.'}


        if len(pt) >= 2 and pt[1] == '*':
            return (self.isMatch(in_str, pt[2:])
                    or first_match and self.isMatch(in_str[1:], pt))
        else:
            return first_match and self.isMatch(in_str[1:], pt[1:])




s = Solution()
print(s.isMatch("ab", "c*ab"))
posted @ 2019-10-15 23:01  黄龙士  阅读(155)  评论(0编辑  收藏  举报