Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Jack_ZhijieFang
2022-08-07 02:02:50 +08:00
committed by GitHub
2 changed files with 85 additions and 0 deletions

View File

@ -351,6 +351,40 @@ func permuteUnique(_ nums: [Int]) -> [[Int]] {
} }
``` ```
### Rust
```Rust
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, used: &mut Vec<bool>) {
let len = nums.len();
if path.len() == len {
result.push(path.clone());
return;
}
for i in 0..len {
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
if used[i] == false {
used[i] = true;
path.push(nums[i]);
Self::backtracking(result, path, nums, used);
path.pop();
used[i] = false;
}
}
}
pub fn permute_unique(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
let mut used = vec![false; nums.len()];
let mut nums= nums;
nums.sort();
Self::backtracking(&mut result, &mut path, &nums, &mut used);
result
}
}
```
### C ### C
```c ```c
//临时数组 //临时数组

View File

@ -423,6 +423,57 @@ function findSubsequences(nums: number[]): number[][] {
}; };
``` ```
### Rust
**回溯+哈希**
```Rust
use std::collections::HashSet;
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize) {
if path.len() > 1 { result.push(path.clone()); }
let len = nums.len();
let mut uset: HashSet<i32> = HashSet::new();
for i in start_index..len {
if (!path.is_empty() && nums[i] < *path.last().unwrap()) || uset.contains(&nums[i]) { continue; }
uset.insert(nums[i]);
path.push(nums[i]);
Self::backtracking(result, path, nums, i + 1);
path.pop();
}
}
pub fn find_subsequences(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
Self::backtracking(&mut result, &mut path, &nums, 0);
result
}
}
```
**回溯+数组**
```Rust
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize) {
if path.len() > 1 { result.push(path.clone()); }
let len = nums.len();
let mut used = [0; 201];
for i in start_index..len {
if (!path.is_empty() && nums[i] < *path.last().unwrap()) || used[(nums[i] + 100) as usize] == 1 { continue; }
used[(nums[i] + 100) as usize] = 1;
path.push(nums[i]);
Self::backtracking(result, path, nums, i + 1);
path.pop();
}
}
pub fn find_subsequences(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
Self::backtracking(&mut result, &mut path, &nums, 0);
result
}
}
```
### C ### C
```c ```c