leetcode 刷题 96 97

leetcode 刷题 96 97

class Solution:
    def numTrees(self, n: int) -> int:
        f={0:1}
        for N in range(1,n+1):
            f[N] = sum([f[i-1]*f[N-i] for i in range(1, N+1)])
        return f[n

class Solution(object):
    def numTrees(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp = [0] * (n+1)
        dp[0] = 1
        dp[1] = 1
        
        for i in range(2,n+1):
            for j in range(1,i+1):
                dp[i] += dp[j-1] * dp[i-j]
        return dp[n]

leetcode 刷题 96 97

leetcode 刷题 96 97

class Solution:
    def isInterleave(self, s1, s2, s3):
        n, m = len(s1), len(s2)
        if len(s3) != n + m:
            return False
        f = [True] + [False] * n
        for i in range(n):
            f[i + 1] = f[i] and s1[i] == s3[i]
        for j in range(m):
            f[0] = f[0] and s2[j] == s3[j]
            for i in range(n):
                f[i + 1] = (f[i] and s1[i] == s3[i + j + 1]) or (f[i + 1] and s2[j] == s3[i + j + 1])
        return f[n]


class Solution:
    
    def isInterleave(self, s1, s2, s3):
        """
        :type s1: str
        :type s2: str
        :type s3: str
        :rtype: bool
        """
        l1, l2, l3 = map(len, [s1, s2, s3])
        if l1 + l2 != l3: return False
        a = set([0])
        for (n, c) in zip(range(l3), s3):
            b = set()
            for i in a:
                if i < l1 and s1[i] == c: b.add(i + 1)
                if n - i < l2 and s2[n - i] == c: b.add(i)
            if len(b) == 0: return False
            a = b
        return True

相关文章:

  • 2022-01-26
  • 2021-10-04
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2021-06-24
  • 2021-12-27
猜你喜欢
  • 2021-12-09
  • 2021-11-30
  • 2021-11-10
  • 2022-12-23
  • 2021-10-21
  • 2021-11-13
  • 2021-11-26
相关资源
相似解决方案