-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-parentheses.rs
43 lines (38 loc) · 1.05 KB
/
generate-parentheses.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
struct Solution;
impl Solution {
fn dfs(s: &str, left: i32, right: i32, n: i32, ans: &mut Vec<String>) {
if left > n || right > left {
return;
}
if s.len() as i32 == n * 2 {
ans.push(s.to_owned());
return;
}
Solution::dfs(&(s.to_owned() + "("), left + 1, right, n, ans);
Solution::dfs(&(s.to_owned() + ")"), left, right + 1, n, ans);
}
pub fn generate_parenthesis(n: i32) -> Vec<String> {
if n <= 0 {
return vec![];
}
let mut ans: Vec<String> = Vec::new();
Solution::dfs("", 0, 0, n, &mut ans);
ans
}
}
fn main() {
println!("{:?}", Solution::generate_parenthesis(1));
println!("{:?}", Solution::generate_parenthesis(3));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert_eq!(Solution::generate_parenthesis(1), ["()"]);
assert_eq!(
Solution::generate_parenthesis(3),
["((()))", "(()())", "(())()", "()(())", "()()()"]
);
}
}