[−][src]Function leetcode_for_rust::cd0022_generate_parentheses::generate_parenthesis
pub fn generate_parenthesis(n: i32) -> Vec<String>
Solutions
Approach 1: Backtracking
-
Time complexity:
-
Space complexity:
impl Solution { pub fn generate_parenthesis(n: i32) -> Vec<String> { let mut result: Vec<String> = vec![]; Self::_gen(&mut result, n, n, "".to_string()); result } pub fn _gen(result: &mut Vec<String>, left: i32, right: i32, sublist: String) { if left == 0 && right == 0 { result.push(sublist); return; } if left > 0 { Self::_gen(result, left - 1, right, sublist.clone() + "("); } if right > left { Self::_gen(result, left, right - 1, sublist.clone() + ")"); } } }