[LeetCode]96. Unique Binary Search Trees
2016-03-07 10:06
471 查看
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.
思路:采用动态规划,n = 0,1一种结果
n = 2 时,两种情况,根节点占一个,左1右0,左0右1;
n = 3时, 三种情况,根节点占一个,左2右0,左1右1,左0右2;
……
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
思路:采用动态规划,n = 0,1一种结果
n = 2 时,两种情况,根节点占一个,左1右0,左0右1;
n = 3时, 三种情况,根节点占一个,左2右0,左1右1,左0右2;
……
public int numTrees(int n) { int[] dp = new int[n+1]; dp[0] = 1; for (int i=1; i<=n; i++) { //int sum = 0; for (int j=1;j<=i;j++) { dp[i] += dp[j-1] * dp[i-j]; } //dp[i] = sum; } return dp ; }
相关文章推荐
- break 和 continue 语句, 以及循环中的 else 子句
- Android常见UI组件之ListView(二)——定制ListView
- FUEL8安装处理
- UINavgationController相关问题
- caffe issues整理
- 面试常客Handler详细解析(更新UI的几种方式)(六)
- 使用uicollectionview 实现单元格滑动吸附效果
- 用WebRequest +HtmlAgilityPack 从外网抓取数据到本地
- easyui input 赋值、取值
- android开发游记:百度地图SDK路线规划和自定义地图UI覆盖物及弹出窗
- YOUTH(年轻)——Samuel Ullman(塞缪尔·乌尔曼)
- page、request、session和application区别 及各自作用域
- UESTC - 1251 谕神的密码 (模拟) 水
- 【DataStructure】Description and usage of queue
- 机房收费系统合作版(三)——UI思索
- POJ 3458 Colour Sequence
- UESTC - 1252 24点游戏 (DFS)好题
- UESTC - 1253 阿里巴巴和n个大盗 (博弈)
- poj 2031 Building a Space Station
- Easyui弹出Dialog时,Dialog中的Combobox的url会加载两次,不明原因,只能其他方法解决。