Unique Binary Search Trees @leetcode

http://oj.leetcode.com/problems/unique-binary-search-trees/

!简单的递归思想,所说是简单的递归思想,但是在打算用递归之前,我也想了挺长时间,努力寻找规律,也努力寻找过F(n-1)到F(n)的递推公式,没有找到合适的结论。脱鞋用递归吧。

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4          if(n == 0)
 5              return 1;
 6          if(n == 1)
 7              return 1;
 8          int res = 0;
 9          int j = 1;
10          for(int i =0;i<n;++i){
11              int x,y;
12              if(i<10000 && a[i] !=0) x = a[i];
13              else x = numTrees(i);
14              if(n-i-1<10000 && a[n-i-1] !=0) y = a[n-i-1];
15              else y = numTrees(n-i-1);
16              res += x*y;
17          }
18          return res;
19      }
20 public:
21     int a[10000];
22 };

=事实证明不用保存状态也可以过,好像没有特别大的数据测试。

原文地址:https://www.cnblogs.com/rogarlee/p/3448015.html