Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 61f4d50

Browse files
committed
src/bin/unique-binary-search-trees.rs
1 parent 373d145 commit 61f4d50

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/bin/unique-binary-search-trees.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fn main() {
2+
println!("{}", Solution::num_trees(3));
3+
println!("{}", Solution::num_trees(4));
4+
println!("{}", Solution::num_trees(19));
5+
}
6+
7+
struct Solution;
8+
9+
impl Solution {
10+
pub fn num_trees(n: i32) -> i32 {
11+
let mut v = vec![0; n as usize + 1];
12+
v[0] = 1;
13+
v[1] = 1;
14+
15+
for i in 2..=n as usize {
16+
for j in 1..=i {
17+
v[i] += v[j - 1] * v[i - j];
18+
}
19+
}
20+
21+
v[n as usize]
22+
}
23+
}

0 commit comments

Comments
 (0)