zoukankan      html  css  js  c++  java
  • 边工作边刷题:70天一遍leetcode: day 95

    Wildcard Matching

    要点:

    • 基本code pattern是以待匹配string s为中心loop,一旦没有任何p的分支match,返回false。如果所有s匹配了,还要检查是不是p也exhausted,一种分支是剩下的p是’*’,这样也可以匹配。
    • worst case: s: abcd, p: efgh? every *, it swallows all abcd. so it O(n^2)

    错误点:

    • star和ss:star对应的是’*’这点在pattern中的位置,ss对应的是在上次没有swallow的s点的位置。实际上是记录了可以restore的状态
    class Solution(object):
        def isMatch(self, s, p):
            """
            :type s: str
            :type p: str
            :rtype: bool
            """
            i,j = 0,0
            ss = -1
            star = -1
            while i<len(s):
                if j<len(p) and (s[i]==p[j] or p[j]=='?'):
                    i+=1;j+=1
                elif j<len(p) and p[j]=='*':
                    star=j; ss=i; j+=1
                elif ss!=-1:
                    ss+=1; i=ss; j=star+1
                else:
                    return False
                    
            while j<len(p) and p[j]=='*': j+=1
            return j==len(p)
    
    
  • 相关阅读:
    C#垃圾回收(GC)
    yum --enablerepo=elrepo-kernel install kernel-lt -y
    centos 查看版本
    linux 内核升级
    awk
    升级内核
    elerpo
    http://elrepo.org/tiki/tiki-index.php
    NO_TITLE
    MongoDB Find查询 1
  • 原文地址:https://www.cnblogs.com/absolute/p/6041366.html
Copyright © 2011-2022 走看看