代码改变世界

leetcode - Unique Binary Search Trees

2013-10-19 21:52  张汉生  阅读(161)  评论(0编辑  收藏  举报

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         int ans = 0;
 6         if (n<=0)
 7             return 0;
 8         int * f = new int[n+1];
 9         f[0] = 1;
10         for (int i=1; i<=n; i++){
11             f[i]=0;
12             for (int j=1; j<=i; j++){
13                 f[i] += f[j-1]*f[i-j];
14             }
15         }
16         ans = f[n];
17         delete []f;
18         return ans;
19     }
20 };