-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47-Permutations_II.rs
More file actions
43 lines (35 loc) · 967 Bytes
/
Copy path47-Permutations_II.rs
File metadata and controls
43 lines (35 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
impl Solution {
pub fn permute_unique(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut res = Vec::new();
let mut path = Vec::new();
let mut used = vec![false; nums.len()];
Self::backtrack(&nums, &mut used, &mut path, &mut res);
res
}
fn backtrack(
nums: &Vec<i32>,
used: &mut Vec<bool>,
path: &mut Vec<i32>,
res: &mut Vec<Vec<i32>>,
) {
if path.len() == nums.len() {
res.push(path.clone());
return;
}
for i in 0..nums.len() {
if used[i] {
continue;
}
if i > 0 && nums[i] == nums[i - 1] && !used[i - 1] {
continue;
}
used[i] = true;
path.push(nums[i]);
Self::backtrack(nums, used, path, res);
path.pop();
used[i] = false;
}
}
}