-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
40 lines (36 loc) · 1012 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
35
36
37
38
39
40
struct Solution {}
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut res: Vec<String> = vec![]; // 存放结果
let mut path = String::from(""); // 存放当前路径, 第一个路径是固定的
back_trace(&mut res, &mut path, 0, 0, n);
println!("{:?}", res);
res
}
}
fn back_trace(res: &mut Vec<String>, path: &mut String, left: i32, right: i32, n: i32) {
if path.len() == 2 * n as usize {
// 递归结束
res.push(path.clone());
return;
}
if left < n {
let mut path = path.clone();
path.push_str("(");
back_trace(res, &mut path, left + 1, right, n)
}
if left > 0 && left > right && right < n {
// 剪枝(左括号可以使用的个数严格大于右括号可以使用的个数,才剪枝
let mut path = path.clone();
path.push_str(")");
back_trace(res, &mut path, left, right + 1, n)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
Solution::generate_parenthesis(3);
}
}