LeetCode *** 96. Unique Binary Search Trees
2016-04-11 10:56
411 查看
题目:
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.
分析:
找规律。
代码:
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
分析:
找规律。
代码:
class Solution { public: int numTrees(int n) { if (n == 0)return 0; int *res = new int[n + 1]; res[0] = 1; res[1] = 1; for (int i = 2; i <= n; ++i) { res[i] = 0; for (int j = i; j>0; --j) { res[i] += res[i - j] * res[j - 1]; } } return res ; } };
相关文章推荐
- 前端框架OnsenUI学习之初识OnsenUI
- map中 key,value 对换 ,互换
- UIView截屏
- 新的博客地址 cuick.cn
- UIPickerView介绍,包括代理函数和常用函数
- iOS uitextfield长度限制
- 设置 UItextField placeholder颜色和字体大小
- 把UIGestureRecognizer 中的点击事件变成Block
- 关于在使用easyui的时候,实现光标定位在easyui-combogrid和easyui-textbox输入框内(也就是获取焦点)
- UIWindow & UIWindowLevel详解
- ZOJ-3929-Deque and Balls【思维】【好题】
- zkui zookeeper web管理
- 52、private static final long serialVersionUID的作用
- 修改UITextField的placeholder颜色
- ZOJ 3929 Deque and Balls
- easyUI的input限制小数输入
- 【Android UI】关于android的UI常识
- iOS学习笔记03-UITableView
- iOS学习笔记02-UIScrollView
- easyui combo+pagination 图标选择器