解题:Leetcode111题:Unique Binary Search Trees

十年热恋 提交于 2020-04-23 22:59:41
package com.leetcode.problem;

/**
 * @author pxu
 * @create 2020-04-23 21:38
 */
public class Problem96 {

    public static void main(String[] args) {

        Problem96 p96 = new Problem96();
        System.out.println(p96.numTrees(3));
        
    }

    public int numTrees(int n) {
        
        if(n < 0)
            return 0;

        int [] recorder = new int[n+1];
        for (int i = 0; i <= n; i++) {
            recorder[i] = -1;
        }

        recorder[0] = 1;
        recorder[1] = 1;

        if(recorder[n] > 0)
            return recorder[n];

        for (int i = 2; i <= n; i++) {

            int res = 0;
            for (int j = 1; j <= i; j++) {
                res = res+(recorder[j-1]*recorder[i-j]);
            }

            recorder[i] = res;

        }

        return recorder[n];
        
    }


}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!