博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-96-Unique Binary Search Trees
阅读量:7104 次
发布时间:2019-06-28

本文共 768 字,大约阅读时间需要 2 分钟。

算法描述:

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

Example:

Input: 3Output: 5Explanation: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

解题思路:动态规划,f(n) = f(0)*f(n-1) +  f(1)*f(n-2) + f(2)*f(n-3) + f(3)*f(n-4) + f(4)*f(n-5) ...... f(n-1)*f(0)

int numTrees(int n) {        vector
dp(n+1,0); dp[0] =1; for(int i = 1; i <= n; i++){ for(int j=0; j < i; j++){ dp[i] += dp[j]*dp[i-j-1]; } } return dp[n]; }

 

转载于:https://www.cnblogs.com/nobodywang/p/10348510.html

你可能感兴趣的文章
【转】 C++文件读写总结
查看>>
关于viewWithTag的使用
查看>>
Vmware vSphere常见问题及解决办法
查看>>
第三次作业
查看>>
C#细节忽略的问题:int 与 int?
查看>>
Ubuntu 安装 uget
查看>>
VC++技术内幕(二)
查看>>
我们做微信二次开发的意义
查看>>
matlab练习程序(PSNR)
查看>>
Debian出现in the drive ‘/media/cdrom/’ and press enter解决办法
查看>>
SkRefCnt
查看>>
1.把一个字符串内的正整数相加
查看>>
日记(二)
查看>>
list,tuple,set,dict基础
查看>>
PIC中的#pragma idata 和#pragma udata
查看>>
使用FileAudit在Windows服务器上实现最优文件访问监控
查看>>
mysql 远程连接数据库的二种方法
查看>>
一步一步学android OpenGL ES2.0编程(4)
查看>>
corosync 源代码分析1
查看>>
寻找Cydia里面软件安装包deb文件的真实下载地址
查看>>