-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46-Permutations.rs
More file actions
36 lines (30 loc) · 917 Bytes
/
Copy path46-Permutations.rs
File metadata and controls
36 lines (30 loc) · 917 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
impl Solution {
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result = Vec::new();
let mut current = Vec::new();
let mut used = vec![false; nums.len()];
fn backtrack(
nums: &Vec<i32>,
current: &mut Vec<i32>,
used: &mut Vec<bool>,
result: &mut Vec<Vec<i32>>,
) {
if current.len() == nums.len() {
result.push(current.clone());
return;
}
for i in 0..nums.len() {
if used[i] {
continue;
}
used[i] = true;
current.push(nums[i]);
backtrack(nums, current, used, result);
current.pop();
used[i] = false;
}
}
backtrack(&nums, &mut current, &mut used, &mut result);
result
}
}