Leetcode 96. Unique Binary Search Trees
2016-04-05 19:10
411 查看
Question
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
Code
public int numTrees(int n) { if (n < 1) { return 1; } int[] nums = new int[n + 1]; nums[0] = 1; nums[1] = 1; for (int i = 2; i <= n; i++) { for (int k = 1; k <= i; k++) { nums[i] += nums[k - 1] * nums[i - k]; } } return nums ; }
相关文章推荐
- Leetcode 95. Unique Binary Search Trees II
- 303-e-Range Sum Query - Immutable
- Uva 10583 Ubiquitous Religions
- org.hibernate.exception.GenericJDBCException: Incorrect string value:
- iOS-精确控制UIButton 的image view与titlelabel的相对位置
- Quicksum-S.B.S.
- [LeetCode]334. Increasing Triplet Subsequence
- Android消息异步机制(ThreadLocal、MessageQueue、Looper、Handler关系总结)
- IOS开发-UI学习-UINavigationController(导航控制器)的使用
- TextruePacker
- iOS 隐藏导航栏UINavigationBar底部黑线
- 使用野狗(Wilddog)云setValue写入数据
- lintcode-medium-Segment Tree Query II
- 【iOS】使用UIView绘制本地验证码
- lintcode-medium-Segment Tree Query
- What's the difference between all the Selection Segues?
- 【iOS】UILabel多行文本的高度计算
- lintcode-medium-Segment Tree Build II
- lintcode-medium-Segment Tree Build
- Material Design UI Widgets —— Palette 调色板