class Solution { public: int numTrees(int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector<int> num; num.push_back(1); num.push_back(1); int temp = 1; for(int i = 2;i<=n;i++) { temp = 0; for(int j = 1;j<=i;j++) { temp += num[j-1] * num[i-j]; } num.push_back(temp); } return temp; } };
唯一搜索树的个数。leetcode上的题,大概思想是递归,但是用数组存储已计算的值可以减少很多重复计算,就像斐波那契数列那样,如果用递归则发费太多的时间。