Thursday, June 12, 2014

LeetCode Unique Binary Search Tree

DP

public int numTrees(int n) {
        int[] numArray = new int[n+1];
        numArray[0] = 1;
        numArray[1] = 1;
        for (int i=2; i<=n; i++) {
            for (int j=0; j<i; j++) {
                numArray[i] += numArray[j]*numArray[i-j-1];
            }
        }
        return numArray[n];
    }

No comments:

Post a Comment