File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
+ * 96. Unique Binary Search Trees
5
+ *
4
6
* Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
5
7
6
8
For example,
15
17
*/
16
18
public class _96 {
17
19
18
- public int numTrees (int n ) {
19
- int [] G = new int [n + 1 ];
20
- G [0 ] = G [1 ] = 1 ;
20
+ public static class Solution1 {
21
+ public int numTrees (int n ) {
22
+ int [] G = new int [n + 1 ];
23
+ G [0 ] = G [1 ] = 1 ;
21
24
22
- for (int i = 2 ; i <= n ; ++i ) {
23
- for (int j = 1 ; j <= i ; ++j ) {
24
- int temp = G [j - 1 ] * G [i - j ];
25
- G [i ] = G [i ] + temp ;
25
+ for (int i = 2 ; i <= n ; ++i ) {
26
+ for (int j = 1 ; j <= i ; ++j ) {
27
+ int temp = G [j - 1 ] * G [i - j ];
28
+ G [i ] = G [i ] + temp ;
29
+ }
26
30
}
31
+ return G [n ];
27
32
}
28
- return G [n ];
29
33
}
30
-
31
34
}
You can’t perform that action at this time.
0 commit comments