20240704 Finished

This commit is contained in:
jackfiled 2024-07-04 10:26:43 +08:00
parent efe3732459
commit 8a6772eb72
2 changed files with 47 additions and 1 deletions

View File

@ -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;
mod p17_letter_combinations_of_a_phone_number;
mod p77_combinations;

View 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() {
}
}