mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1531 from cezarbbb/Backtracking04
添加 0090.子集II Rust版本
This commit is contained in:
@ -367,6 +367,36 @@ function subsetsWithDup(nums: number[]): number[][] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
impl Solution {
|
||||||
|
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize, used: &mut Vec<bool>) {
|
||||||
|
result.push(path.clone());
|
||||||
|
let len = nums.len();
|
||||||
|
// if start_index >= len { return; }
|
||||||
|
for i in start_index..len {
|
||||||
|
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
|
||||||
|
path.push(nums[i]);
|
||||||
|
used[i] = true;
|
||||||
|
Self::backtracking(result, path, nums, i + 1, used);
|
||||||
|
used[i] = false;
|
||||||
|
path.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subsets_with_dup(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, 0, &mut used);
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### C
|
### C
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
Reference in New Issue
Block a user