20240705 Finished

This commit is contained in:
jackfiled 2024-07-05 10:10:47 +08:00
parent 8a6772eb72
commit e3590fe741
2 changed files with 51 additions and 1 deletions

View File

@ -166,4 +166,5 @@ 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 p77_combinations;
mod p77_combinations;
mod p46_permutations;

View File

@ -0,0 +1,49 @@
/**
* [46] Permutations
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result = vec![];
let mut output = nums.clone();
Self::search(output.len(), &mut output, &mut result, 0);
result
}
fn search(length: usize, output: &mut Vec<i32>, result: &mut Vec<Vec<i32>>, pos: usize) {
if length == pos {
result.push(output.clone());
return;
}
for i in pos..length {
Self::swap(output, i, pos);
Self::search(length, output, result, pos + 1);
Self::swap(output, i, pos);
}
}
fn swap(output: &mut Vec<i32>, a: usize, b: usize) {
let temp = output[a];
output[a] = output[b];
output[b] = temp;
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_46() {
}
}