forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.rs
28 lines (24 loc) · 839 Bytes
/
Solution2.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
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut dp: Vec<Vec<String>> = vec![vec![]; n as usize + 1];
// Initialize the dp vector
dp[0].push(String::from(""));
dp[1].push(String::from("()"));
// Begin the actual dp process
for i in 2..=n as usize {
for j in 0..i as usize {
let dp_c = dp.clone();
let first_half = &dp_c[j];
let second_half = &dp_c[i - j - 1];
for f in first_half {
for s in second_half {
let f_c = f.clone();
let cur_str = f_c + "(" + &*s + ")";
dp[i].push(cur_str);
}
}
}
}
dp[n as usize].clone()
}
}