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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章