LeetCode-44-Wildcard Matching DP

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
        



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值