Unique Binary Search Trees
Catalan number version - 32ms
1 2 3 4 5 6 7 8 9 10
| class Solution: def numTrees(self, n): """ :type n: int :rtype: int """ ans = 1 for i in range(1, n + 1): ans = ans * (i + n) // i return ans // (n + 1)
|
test code
1 2
| In [95]: s = Solution(); t = s.numTrees(3); print(t) 5
|
leet code
卡塔兰数