leetcode: Unique Binary Search Trees

来源:互联网 发布:apache ab.exe打不开 编辑:程序博客网 时间:2024/06/10 19:46

卡特兰数,直接用公式求

不过要注意数的大小上溢,用long long

也可以用动态规划模拟h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

class Solution {public:    int numTrees(int n) {        long long a = factorial(n+1, 2*n);        long long b = factorial(1, n+1);        return a/b;    }    long long  factorial( int n, int m){        long long res = 1;        for( int i = n; i <= m; ++i){            res *= i;        }        return res;    }};


0 0
原创粉丝点击