20240704 Finished
This commit is contained in:
parent
efe3732459
commit
8a6772eb72
|
@ -165,4 +165,5 @@ mod p127_word_ladder;
|
||||||
mod p208_implement_trie_prefix_tree;
|
mod p208_implement_trie_prefix_tree;
|
||||||
mod p211_design_add_and_search_words_data_structure;
|
mod p211_design_add_and_search_words_data_structure;
|
||||||
mod p212_word_search_ii;
|
mod p212_word_search_ii;
|
||||||
mod p17_letter_combinations_of_a_phone_number;
|
mod p17_letter_combinations_of_a_phone_number;
|
||||||
|
mod p77_combinations;
|
45
src/problem/p77_combinations.rs
Normal file
45
src/problem/p77_combinations.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/**
|
||||||
|
* [77] Combinations
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
|
||||||
|
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<i32>, result: &mut Vec<Vec<i32>>) {
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user