Skip to content

Commit

Permalink
Create 0046-permutations.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajvenkat authored Jan 25, 2023
1 parent 9da072d commit 5968b8f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rust/0046-permutations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
fn backtrack(first: usize, result: &mut Vec<Vec<i32>>, nums: &mut Vec<i32>) {
if first == nums.len() {
result.push(nums.to_owned());
return;
}

for i in first..nums.len() {
nums.swap(first, i);
Solution::backtrack(first + 1, result, nums);
nums.swap(first, i);
}
}

pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
let (mut result, mut nums) = (vec![], nums);
Self::backtrack(0, &mut result, &mut nums);

result
}
}

0 comments on commit 5968b8f

Please sign in to comment.