第五天

来源:互联网 发布:会员卡系统java源代码 编辑:程序博客网 时间:2024/04/28 13:37

第五天

今天leetcode 晚上的时候挂掉了。。

今天主要的时间都用来写报告了

图形学报告,,,c#报告。。。。

本来想非递归实现的,最后死于如果s都被匹配上p是否呗用完的纠结上。。

最后还是递归实现吧。

def isMatch(s, p):    len1 = len(s)    len2 = len(p)    dp = []    dp2 = []    dp.append(1)    dp2.append(1)    for i in range(len1):        dp.append(0)        dp2.append(0)    for x in range(1,len2+1):        if p[x-1] == '*':            continue        if x<len2:            if p[x]=='*':                for y in range(1,len1+1):                    if s[y-1] == p[x-1]or p[x-1] == '.':                        dp[y] = max(dp2[y-1],dp[y-1])                temp = dp2                dp2 = dp                dp = temp                continue        for y in range(1,len1+1):            if s[y-1] == p[x-1] or p[x-1] == '.':                dp[y] = dp2[y-1]        print dp        temp = dp2        dp2 = dp        dp = temp    if dp[len1] == 1:        return True    else:        return Falseprint isMatch('aaa','aaaa')

递归实现代码

class Solution:    # @param {string} s    # @param {string} p    # @return {boolean}    def isMatch(self, s, p):        if not s and not p:            return True        if not p and s:            return False        if p[-1] == '*':            rep = p[-2]            if s and (s[-1] == rep or rep == '.'):                return self.isMatch(s[:-1], p) or self.isMatch(s, p[:-2])             else:                return self.isMatch(s, p[:-2])        else:            if s and (p[-1] == s[-1] or p[-1] == '.'):                return self.isMatch(s[:-1], p[:-1])            else:                return False

明天继续研究这个题, 一定要非递归实现成功一次,贵在坚持

0 0
原创粉丝点击