-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlib.rs
34 lines (33 loc) · 785 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* @lc app=leetcode.cn id=96 lang=rust
*
* [96] 不同的二叉搜索树
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn num_trees(n: i32) -> i32 {
// 题解https://leetcode-cn.com/problems/unique-binary-search-trees/solution/bu-tong-de-er-cha-sou-suo-shu-by-leetcode-solution/
let mut dp = vec![0; (n + 1) as usize];
dp[0] = 1;
dp[1] = 1;
for i in 2..=n {
for j in 1..=i {
let i = i as usize;
let j = j as usize;
dp[i] += dp[j - 1] * dp[i - j];
}
}
println!("{:?}", dp);
dp[n as usize]
}
}
// @lc code=end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
Solution::num_trees(3);
}
}