LeetCode动态规划

目录

10. Regular Expression Matching

70. Climbing Stairs

72. Edit Distance

322. Coin Change


10. Regular Expression Matching

这个问题主要是正则规则的理解,多submit几次就懂了【滑稽】。

需要关注的判断条件是s[i],p[j]是否相等,p[j+1]是否为‘*’,p[j]是否为'.'。

只有两个字符串都遍历完了的情况,这时可以返回true。

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        l1,l2=len(s),len(p)
        def match(i,j):
            if i==l1 and j==l2:
                return True
            elif i<l1 and j==l2:
                return False
            elif i==l1 and j<l2:
                if j+1==l2:
                    return False
                elif p[j+1]=='*':
                    return match(i,j+2)
                else:
                    return False
            if j+1==l2:
                if p[j]=='.' or s[i]==p[j]:
                    return match(i+1,j+1)
                else:
                    return False
            if s[i]!=p[j]:
                if p[j+1]=='*':
                    if p[j]=='.':
                        return match(i,j+2) or match(i+1,j)
                    else:
                        return match(i,j+2)
                else:
                    if p[j]=='.':
                        return match(i+1,1+j)
                    else:
                        return False
            else:
                if p[j+1]=='*':
                    return match(i+1,j) or match(i,j+2)
                else:
                    return match(i+1,1+j)
        return match(0,0)

70. Climbing Stairs

设d(x)为金额为x时爬楼梯的方式数目

  1. 初始条件:d(0)=1,x<0时,d(x)=0。或者是d(1)=1,d(2)=2,x<1时,d(x)=0
  2. 递归方程:d(x)=d(x-1)+d(x-2)
  3. 优化方式:将已经计算好的d(x)存起来,防止多次计算同一个值
class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        a={}
        def d(x):
            if a.__contains__(x):
                return a[x]
            res=0
            if x==0:
                res= 1
            elif x<0:
                return 0
            else:
                res= d(x-1)+d(x-2)
            a[x]=res
            return res
        return d(n)

72. Edit Distance

关键是递归关系,因为置换、删除和插入都需要一次,所以递归方程如下:

if word1[i-1]==word2[j-1]:
    dp[i][j]=dp[i-1][j-1]
else:
    dp[i][j]=1+min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        l1,l2=len(word1),len(word2)
        dp=[[0]*(l2+1) for _ in range(l1+1)]
        print(dp)
        for j in range(1,l2+1):  
            dp[0][j]=dp[0][j-1]+1
        for i in range(1,l1+1):
            dp[i][0]=dp[i-1][0]+1

        print(dp)
        for i in range(1,l1+1):
            for j in range(1,l2+1):                 
                if word1[i-1]==word2[j-1]:
                    dp[i][j]=dp[i-1][j-1]
                else:
                    dp[i][j]=1+min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
                # print(i,j,dp[i][j])
        return dp[l1][l2]

322. Coin Change

和第70题思路一样。

class Solution(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        
        t={}
        def g(a):
            if t.__contains__(a):
                return t[a]
            elif a==0:
                return 0
            elif a<0:
                return -1
            else:
                c=[g(a-i) for i in coins if g(a-i)>=0]
                if c==[]:
                    res = -1
                else:
                    res=min(c)+1

            t[a]=res
            return res
        return g(amount)
发布了10 篇原创文章 · 获赞 6 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_35565669/article/details/105223067