-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_0022_generate_parentheses.rs
More file actions
36 lines (33 loc) · 1009 Bytes
/
_0022_generate_parentheses.rs
File metadata and controls
36 lines (33 loc) · 1009 Bytes
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
struct Solution;
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut result: Vec<String> = vec![];
let mut current: Vec<char> = vec![];
Self::find(&mut result, &mut current, n, n);
print!("{:?}", result);
result
}
fn find(result: &mut Vec<String>, current: &mut Vec<char>, left: i32, right: i32) {
if left == 0 && right == 0 {
result.push(current.iter().collect::<String>());
} else {
if left > 0 {
current.push('(');
Self::find(result, current, left - 1, right);
current.pop();
}
if right > left {
current.push(')');
Self::find(result, current, left, right - 1);
current.pop();
}
}
}
}
#[test]
fn test() {
assert_eq!(
Solution::generate_parenthesis(3),
vec_string!["((()))", "(()())", "(())()", "()(())", "()()()"]
);
}