Skip to content

Commit 22f881c

Browse files
committed
solution: Generate Parentheses
1 parent 310caca commit 22f881c

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
name = "leetcode"
33
version = "0.1.0"
44
authors = ["Kilerd Chan <[email protected]>"]
5-
edition = "2018"
6-
7-
[workspace]
5+
edition = "2018"

src/generate_parentheses.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/// @number 22
2+
/// @title Generate Parentheses
3+
/// @url https://leetcode.com/problems/generate-parentheses/
4+
/// @difficulty Medium
5+
6+
struct Solution;
7+
8+
impl Solution {
9+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
10+
if n == 0 {
11+
return vec!["".to_string()];
12+
}
13+
let mut ans = vec![];
14+
for i in 0..=n - 1 {
15+
for left in Solution::generate_parenthesis(n - 1 - i) {
16+
for right in Solution::generate_parenthesis(i) {
17+
ans.push(format!("({}){}", left, right));
18+
}
19+
}
20+
}
21+
ans
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod test {
27+
use crate::generate_parentheses::Solution;
28+
29+
#[test]
30+
fn test() {
31+
let expected = vec!["((()))", "(()())", "(())()", "()(())", "()()()"];
32+
let expected: Vec<String> = expected.into_iter().map(|item| item.to_string()).collect();
33+
assert_eq!(expected, Solution::generate_parenthesis(3));
34+
}
35+
#[test]
36+
fn test_n_equals_0() {
37+
let expected: Vec<String> = vec!["".to_string()];
38+
assert_eq!(expected, Solution::generate_parenthesis(0));
39+
}
40+
}

src/main.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
mod list;
2-
mod zigzag_conversion;
31
mod add_two_number;
4-
mod jewels_and_stones;
5-
mod nim_game;
6-
mod unique_email_addresses;
7-
mod to_lower_case;
8-
mod unique_morse_code_words;
9-
mod sort_array_by_parity;
10-
mod robot_return_to_origin;
11-
mod hamming_distance;
12-
mod di_string_match;
2+
mod array_partition_i;
133
mod big_countries;
4+
mod container_with_most_water;
5+
mod di_string_match;
6+
mod divide_two_integers;
147
mod flipping_an_image;
8+
mod generate_parentheses;
9+
mod hamming_distance;
10+
mod implement_strstr;
11+
mod integer_to_roman;
12+
mod jewels_and_stones;
13+
mod letter_combinations_of_a_phone_number;
14+
mod list;
15+
mod longest_substring_without_repeating_characters;
1516
mod merge_two_binary_trees;
16-
mod two_sum;
17+
mod nim_game;
18+
mod number_of_recent_calls;
19+
mod palindrome_number;
1720
mod peak_index_in_a_mountain_array;
21+
mod remove_duplicates_from_sorted_array;
22+
mod remove_element;
23+
mod reverse_integer;
24+
mod robot_return_to_origin;
1825
mod self_dividing_numbers;
19-
mod array_partition_i;
20-
mod number_of_recent_calls;
26+
mod sort_array_by_parity;
2127
mod sort_array_by_parity_ii;
22-
mod longest_substring_without_repeating_characters;
2328
mod sort_colors;
24-
mod reverse_integer;
25-
mod integer_to_roman;
2629
mod string_to_integer_atoi;
27-
mod palindrome_number;
28-
mod container_with_most_water;
2930
mod three_sum;
3031
mod three_sum_closest;
31-
mod letter_combinations_of_a_phone_number;
32+
mod to_lower_case;
33+
mod two_sum;
34+
mod unique_email_addresses;
35+
mod unique_morse_code_words;
3236
mod valid_parentheses;
33-
mod remove_duplicates_from_sorted_array;
34-
mod remove_element;
35-
mod implement_strstr;
36-
mod divide_two_integers;
37+
mod zigzag_conversion;
38+
39+
fn main() {
40+
println!("hello leetcode");
41+
}

0 commit comments

Comments
 (0)