Skip to content

Commit 5968b8f

Browse files
authored
Create 0046-permutations.rs
1 parent 9da072d commit 5968b8f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

rust/0046-permutations.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
fn backtrack(first: usize, result: &mut Vec<Vec<i32>>, nums: &mut Vec<i32>) {
3+
if first == nums.len() {
4+
result.push(nums.to_owned());
5+
return;
6+
}
7+
8+
for i in first..nums.len() {
9+
nums.swap(first, i);
10+
Solution::backtrack(first + 1, result, nums);
11+
nums.swap(first, i);
12+
}
13+
}
14+
15+
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
16+
let (mut result, mut nums) = (vec![], nums);
17+
Self::backtrack(0, &mut result, &mut nums);
18+
19+
result
20+
}
21+
}

0 commit comments

Comments
 (0)