public class Solution { /** * @param n: An integer * @return: An integer */ public int numTrees(int n) { // write your code here int[] count = new int[n+1]; if(n <= 1){ return 1; } count[0] = 1; count[1] = 1; for(int i = 2; i < n+1; i++){ for(int j = 0; j < i; j++){ count[i] += count[j] * count[i-j-1]; } } return count[n]; }
Saturday, February 17, 2018
163. Unique Binary Search Trees
以某个点i作为根节点时,BST的数目为i左边所有点的BST的个数 * i右边所有点的BST的个数
定义Count[i]为i个数能产生的Unique Binary Tree的数目,
{}
0个Node
Count[0] = 1
{1}
1个Node
Count[1] = 1
{1, 2}
2个Node
Count[2] = Count[0] * Count[1]
+ Count[1] * Count[0]
{1,2,3}
3个Node
Count[3] = Count[0] * Count[2] // root = 1, 2 and 3 are on the right side
+ Count[1] * Count[1] // root = 2, 1 is on the left, and 3 is on the right side
+ Count[2] * Count[0] // root = 3, 1 and 2 are on the left side
Subscribe to:
Post Comments (Atom)
4. Ugly Number
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
-
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
-
Question: Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digit...
-
Thinking: binary Tree 和 Stringbuilder 的联合题 对Stringbuilder 和StringTokenizer的使用方法的考验 public String serialize(TreeNode root) { ...
No comments:
Post a Comment