leetcode Unique Binary Search Trees
class Solution { public: int numTrees(int n) { vector<int> v(n+1, 0); v[0] = 1; v[1] = 1; v[2] = 2; if(n<=2) return v[n]; for(int i=3; i<=n; i++) for(int j=1; j<=i; j++) v[i] += v[j-1]*v[i-j]; return v[n]; } };
class Solution { public: int numTrees(int n) { vector<int> v(n+1, 0); v[0] = 1; v[1] = 1; v[2] = 2; if(n<=2) return v[n]; for(int i=3; i<=n; i++) for(int j=1; j<=i; j++) v[i] += v[j-1]*v[i-j]; return v[n]; } };