-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
54 lines (49 loc) · 1.22 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
fn main() {
println!("Hello, world!");
}
struct Solution {}
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut result = vec![];
let mut substring = "".to_string();
Self::gen(&mut result, n, n, &mut substring);
result
}
fn gen(res: &mut Vec<String>, l: i32, r: i32, front: &mut String) {
if l == 0 && r == 0 {
res.push(front.to_string());
return
}
if l == r {
front.push('(');
Self::gen(res, l - 1, r, front);
front.pop();
} else if l < r {
if l != 0 {
front.push('(');
Self::gen(res, l - 1, r, front);
front.pop();
}
front.push(')');
Self::gen(res, l, r - 1, front);
front.pop();
}
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(
Solution::generate_parenthesis(3),
vec![
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
].iter().map(|x| x.to_string()).collect::<Vec<String>>()
);
}
}