美文网首页
96. Unique Binary Search Trees

96. Unique Binary Search Trees

作者: April63 | 来源:发表于2018-07-03 14:27 被阅读0次
这一题用了卡特兰数, 微信图片_20180703142700.jpg

代码如下:

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

相关文章

网友评论

      本文标题:96. Unique Binary Search Trees

      本文链接:https://www.haomeiwen.com/subject/efoduftx.html