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.
看得别人的代码,然后写出来的 :http://www.2cto.com/kf/201312/262420.html
public class UniqueBinarySearchTrees { public static void main(String[] args) {
int n = (int)(Math.random()*100); // 随机生成数字
System.out.println(n);
int num = numTrees(n);
System.out.println(num);
} private static int numTrees(int n) {
if (n == 0 || n == 1) {
return n;
}
else {
// 以i为根节点时,其左子树构成为[0,...,i-1],其右子树构成为[i+1,...,n]构成
// 因此,dp[i] = sigma(dp[0...k] * dp[k+1...i]) 0 <= k < i - 1
// dp[3] = dp[0] * dp[2] + dp[1] * dp[1] + dp[2] * dp[0]
int[] num = new int[n+1];
num[0] = num[1] = 1;
for (int i = 2; i < num.length; i++) {
num[i] = 0;
for (int j = 0; j < i; j++) {
num[i] = num[i] + num[j] * num[i-j-1];
}
}
return num[n];
}
} }
Unique Binary Search Trees的更多相关文章
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- Unique Binary Search Trees I & II
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...
随机推荐
- dwz的form表单中url的变量替换
form表单中action的地址格式 “__URL__/edit/{xxx}”,大括号内的 “xxx” 就是变量名,主要功能是结合table组件一起使用. 下图中的删除.编辑.修改密码都是用了url变 ...
- DOM对象与jquery对象的区别
jQuery对象和DOM对象使用说明,需要的朋友可以参考下. 1.jQuery对象和DOM对象 第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQue ...
- 如果 if - 迈克.杰克逊的墓志铭
引用http://www.duwenzhang.com/wenzhang/yingyuwenzhang/20110215/171059.html IF you can keep your head w ...
- 20145223 《Java程序程序设计》实验报告4
20145223杨梦云Java实验四<Andoid开发基础> 实验内容 安装Android Studio 运行安卓AVD模拟器 使用Android运行出模拟手机并显示自己的学号 实验步骤 ...
- appium运行报错.<init>(Lorg/openqa/selenium/remote/ErrorCodes;Z)V
最近这几天就在学习appium,搭建环境就耗费了很多时间,不得不承认自己够笨的了,然后我把环境搭建好,写完脚本的时候,就报这个错了,当时是从某个群里直接下载的demo,不得不吐槽说,够坑的,是能跑通, ...
- DP ZOJ 2745 01-K Code
题目传送门 题意:要求任意连续子序列中0和1的数量差不超过k的方案数 分析:想好状态其实不难.dp[i][j][k]表示考虑前i长度,后缀中最大的 sum(0) - sum(1) = j, sum ( ...
- Codeforces Round #337 (Div. 2)
水 A - Pasha and Stick #include <bits/stdc++.h> using namespace std; typedef long long ll; cons ...
- stl(set+stack) LA 3634 The SetStack Computer
题目传送门 题意:给一些对集合的操作,询问每一次操作后栈顶的集合元素个数 分析:首先{}是空的,每一次add时候,{} -> { {} }变成了有一个元素的集合,利用set和stack,map容 ...
- PHP、Java对称加密中的AES加密方法
PHP AES加密 <?php ini_set('default_charset','utf-8'); class AES{ public $iv = null; public $key = n ...
- iOS 'The sandbox is not sync with the Podfile.lock错误
出现以下错误时, diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or dire ...