GZY的博客

There is no failure, only feedback

leetcode:96. Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

/**
 * @param {number} n
 * @return {number}
 */
var numTrees = function(n) {
    var map = [1,1];
    for(var i = 2; i <= n; i++) {
        map[i] = 0;
        for(var j = 1; j <= i; j++) {
                map[i] += (map[j-1] * map[i-j])
            }
    }
    return map[n];
};
动态规划的题,尝试了下用js来写

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012042456/article/details/51553853
个人分类: leetcode解题报告
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭