c++ dp solution


  • 0
    X
    class Solution {
    public:
        //dp
        int numTrees(int n) {
            if(n <= 0){
                return 0;
            }
            vector<int> f(n + 1);
            f[0] = 1;
            for(int i = 1; i <= n; ++i){
                for(int j = 1; j <= i; ++j){
                    f[i] += f[j - 1] * f[i - j];
                }
            }
            return f[n];
        }
    };
    

Log in to reply
 

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