diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 92659b1..b7bcd4e 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -165,4 +165,5 @@ mod p127_word_ladder; mod p208_implement_trie_prefix_tree; mod p211_design_add_and_search_words_data_structure; mod p212_word_search_ii; -mod p17_letter_combinations_of_a_phone_number; \ No newline at end of file +mod p17_letter_combinations_of_a_phone_number; +mod p77_combinations; \ No newline at end of file diff --git a/src/problem/p77_combinations.rs b/src/problem/p77_combinations.rs new file mode 100644 index 0000000..225ca5c --- /dev/null +++ b/src/problem/p77_combinations.rs @@ -0,0 +1,45 @@ +/** + * [77] Combinations + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result = vec![]; + let mut now = Vec::with_capacity(k as usize); + + Self::search(n, k, 1, &mut now, &mut result); + + result + } + + fn search(n: i32, k: i32, x: i32, now: &mut Vec, result: &mut Vec>) { + if now.len() as i32 + (n - x + 1) < k { + return; + } + + if now.len() as i32 >= k { + result.push(now.clone()); + return; + } + + now.push(x); + Self::search(n, k, x + 1, now, result); + now.pop(); + Self::search(n, k, x + 1, now, result); + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_77() { + } +}