mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #1923 from fwqaaq/patch-6
Update 回溯算法去重问题的另一种写法.md about rust
This commit is contained in:
@ -525,10 +525,122 @@ function permuteUnique(nums: number[]): number[][] {
|
||||
};
|
||||
```
|
||||
|
||||
Go:
|
||||
Rust:
|
||||
|
||||
**90.子集II**:
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
let mut res = vec![];
|
||||
let mut path = vec![];
|
||||
nums.sort();
|
||||
Self::backtracking(&nums, &mut path, &mut res, 0);
|
||||
res
|
||||
}
|
||||
|
||||
pub fn backtracking(
|
||||
nums: &Vec<i32>,
|
||||
path: &mut Vec<i32>,
|
||||
res: &mut Vec<Vec<i32>>,
|
||||
start_index: usize,
|
||||
) {
|
||||
res.push(path.clone());
|
||||
let mut helper_set = HashSet::new();
|
||||
for i in start_index..nums.len() {
|
||||
if helper_set.contains(&nums[i]) {
|
||||
continue;
|
||||
}
|
||||
helper_set.insert(nums[i]);
|
||||
path.push(nums[i]);
|
||||
Self::backtracking(nums, path, res, i + 1);
|
||||
path.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**40. 组合总和 II**
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn backtracking(
|
||||
candidates: &Vec<i32>,
|
||||
target: i32,
|
||||
sum: i32,
|
||||
path: &mut Vec<i32>,
|
||||
res: &mut Vec<Vec<i32>>,
|
||||
start_index: usize,
|
||||
) {
|
||||
if sum > target {
|
||||
return;
|
||||
}
|
||||
if sum == target {
|
||||
res.push(path.clone());
|
||||
}
|
||||
let mut helper_set = HashSet::new();
|
||||
for i in start_index..candidates.len() {
|
||||
if sum + candidates[i] <= target {
|
||||
if helper_set.contains(&candidates[i]) {
|
||||
continue;
|
||||
}
|
||||
helper_set.insert(candidates[i]);
|
||||
path.push(candidates[i]);
|
||||
Self::backtracking(candidates, target, sum + candidates[i], path, res, i + 1);
|
||||
path.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn combination_sum2(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
|
||||
let mut res = vec![];
|
||||
let mut path = vec![];
|
||||
candidates.sort();
|
||||
Self::backtracking(&candidates, target, 0, &mut path, &mut res, 0);
|
||||
res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**47. 全排列 II**
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn permute_unique(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
let mut res = vec![];
|
||||
let mut path = vec![];
|
||||
let mut used = vec![false; nums.len()];
|
||||
Self::backtracking(&mut res, &mut path, &nums, &mut used);
|
||||
res
|
||||
}
|
||||
pub fn backtracking(
|
||||
res: &mut Vec<Vec<i32>>,
|
||||
path: &mut Vec<i32>,
|
||||
nums: &Vec<i32>,
|
||||
used: &mut Vec<bool>,
|
||||
) {
|
||||
if path.len() == nums.len() {
|
||||
res.push(path.clone());
|
||||
return;
|
||||
}
|
||||
let mut helper_set = HashSet::new();
|
||||
for i in 0..nums.len() {
|
||||
if used[i] || helper_set.contains(&nums[i]) {
|
||||
continue;
|
||||
}
|
||||
helper_set.insert(nums[i]);
|
||||
path.push(nums[i]);
|
||||
used[i] = true;
|
||||
Self::backtracking(res, path, nums, used);
|
||||
used[i] = false;
|
||||
path.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user