dp solution with python


  • 0
    G
    def numTrees(self, n):
            """
            :type n: int
            :rtype: int
            """
            dp = [0 for _ in xrange(n + 1)]
            for i in xrange(n + 1):
                if i == 1 or i == 0:
                    dp[i] = 1
                else:
                    for j in xrange(1, i + 1):
                        dp[i] += (dp[i - j] * dp[j - 1])
            return dp[n]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.