LeetCode-44-Wildcard Matching DP

来源:互联网 发布:上海大学乐乎 编辑:程序博客网 时间:2024/06/04 18:56

dp[i][j]指s的前i个和p的前j个是否匹配,dp[0][0]指s和p为空时,初始化为True,其他全为F

把p开头的所有的*都找出来,初始化dp[0][j]为T,因为后面dp的时候就从1开始了,不更新0了

然后就是根据p[j]分类讨论就行


DP:

"""public boolean isMatch_2d_method(String s, String p) {int m=s.length(), n=p.length();boolean[][] dp = new boolean[m+1][n+1];dp[0][0] = true;for (int i=1; i<=m; i++) {dp[i][0] = false;}for(int j=1; j<=n; j++) {if(p.charAt(j-1)=='*'){dp[0][j] = true;} else {break;}}for(int i=1; i<=m; i++) {for(int j=1; j<=n; j++) {if (p.charAt(j-1)!='*') {dp[i][j] = dp[i-1][j-1] && (s.charAt(i-1)==p.charAt(j-1) || p.charAt(j-1)=='?');} else {dp[i][j] = dp[i-1][j] || dp[i][j-1];}}}return dp[m][n];}"""class Solution(object):      def isMatch(self, s, p):          """         :type s: str         :type p: str         :rtype: bool         """          Lens=len(s)        Lenp=len(p)        dp=[[False for x in range(Lenp+1)]for y in range(Lens+1)]        dp[0][0]=True        for i in range(0,Lenp):            if p[i]=='*':dp[0][i+1]=True            else:break        for i in range(1,Lens+1):            for j in range(1,Lenp+1):                if p[j-1]=='*':                    dp[i][j]=dp[i][j-1] or dp[i-1][j-1] or dp[i-1][j]                else:                    dp[i][j]=(s[i-1]==p[j-1] or p[j-1]=='?') and dp[i-1][j-1]        return dp[Lens][Lenp]                        


记忆化递归搜索,T了,挂在最后一组数据,讲道理时间复杂度是一样的。。。

class Solution(object):      dp=[]    def match(self, s, p):          """         :type s: str         :type p: str         :rtype: bool         """          Lens=len(s)        Lenp=len(p)        #print "Len",Lens,Lenp        #if Lens==1 and Lenp==2:print "???",self.dp        #print Lens,Lenp        if self.dp[Lens][Lenp]!=-1:            #print Lens,Lenp            return self.dp[Lens][Lenp]        if p=="":              if s=="":self.dp[0][0]=1            else:self.dp[0][0]=0            return s==""          if len(p)==1:              if p[0]=='*':                self.dp[Lens][1]=1                return True            if len(s)==1 and (s[0]==p[0] or p[0]=='?' ):                  self.dp[1][1]=1                return True              else:                  self.dp[Lens][1]=0                return False          if p[0]=='*' and p[1]=='*':            self.dp[Lens][Lenp-1]=self.match(s,p[1:])            return self.dp[Lens][Lenp-1]        if p[0]=='*':              self.dp[Lens][Lenp-1]=self.match(s,p[1:])            if(self.dp[Lens][Lenp-1]):                  self.dp[Lens][Lenp]=1                return True              temS=s              while len(temS)>0:                  self.dp[len(temS)-1][Lenp-1]=self.match(temS[1:],p[1:])                if(self.dp[len(temS)-1][Lenp-1]):                      return True                  temS=temS[1:]              self.dp[Lens][Lenp]=0            return False          else:              if len(s)>0 and (s[0]==p[0] or p[0]=='?'):                  #print "fuck",self.dp                #print Lens-1,Lenp-1,self.dp[Lens-1][Lenp-1]                #print s[1:]                #print p[1:]                self.dp[Lens-1][Lenp-1]=self.match(s[1:],p[1:])                  #print "A",a                #print self.dp,Lens-1,Lenp-1                #self.dp[Lens-1][Lenp-1]=self.match(s[1:],p[1:])                  return self.dp[Lens-1][Lenp-1]             self.dp[Lens][Lenp]=0            return False     def isMatch(self, s, p):        Lens=len(s);        Lenp=len(p)        self.dp=[[-1 for x in range(Lenp+1)]for y in range(Lens+1)]        #print self.dp        if self.match(s,p):            return True        return False        



原创粉丝点击