Skip to content

Commit

Permalink
[csharp] 46. Permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
benjiwright committed Sep 28, 2022
1 parent 341ddaa commit 619aad9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions csharp/46-Permutations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Solution
{
public IList<IList<int>> Permute(int[] nums)
{
var result = new List<IList<int>>();
PermuteRecurse(result, nums, 0);
return result;
}

private void PermuteRecurse(List<IList<int>> res, int[] arr, int start)
{
if (start == arr.Length)
{
var list = arr.Select(t => (t)).ToList();
res.Add(list);
return;
}

for (var i = start; i < arr.Length; i++)
{
(arr[start], arr[i]) = (arr[i], arr[start]);
PermuteRecurse(res, arr, start + 1);
(arr[start], arr[i]) = (arr[i], arr[start]);
}
}
}

0 comments on commit 619aad9

Please sign in to comment.