mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0047.全排列II Rust版本
添加 0047.全排列II Rust版本
This commit is contained in:
@ -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
|
||||
//临时数组
|
||||
|
Reference in New Issue
Block a user