题目描述
方法思路
这是一个动态规划问题;
class Solution {
//Runtime: 0 ms, faster than 100.00%
//Memory Usage: 31.7 MB, less than 100.00%
public int numTrees(int n) {
if(n == 0 || n == 1) return 1;
int[] ans = new int[n + 1];
ans[0] = 1;
ans[1] = 1;
for(int i = 2; i <= n; i++){
for(int j = 1; j <= i; j++){
ans[i] += ans[j - 1] * ans[i - j];
}
}
return ans[n];
}
}