The problem is to return a list containing all permutations of an array. For some reason the result list is empty. The truth is I'm 100% sure it has to do with me using IList or List wrong somewhere, but it was honestly confusing to have to return it as an IList<IList<int>>
...I feel like it would have been easier to just return List<List<int>>
but I'm doing this off of leetcode and trying to keep the original signature that was written in the question. When currentPermutation.Count == nums.Length, I printed the values of the currnet permutation and it printed all permutations, so I know that currentPermutation is being filled with the corrent numbers..why isn't the result list doing it as well?
public class Solution {
public IList<IList<int>> Permute(int[] nums) {
List<IList<int>> listOfPermutations = new List<IList<int>>();
IList<int> currentPermutation = new List<int>();
int[] elementsSeen = new int[nums.Length];
Permute(listOfPermutations, nums, elementsSeen, currentPermutation);
return (IList<IList<int>>)listOfPermutations;
}
public void Permute(List<IList<int>> list, int[] nums, int[] elementsSeen, IList<int> currentPermutation) {
if (currentPermutation.Count == nums.Length) {
list.Add(currentPermutation);
}
else {
for (int i = 0; i < nums.Length; i++) {
if (elementsSeen[i] == 0) {
elementsSeen[i] = 1;
currentPermutation.Add(nums[i]);
Permute(list, nums, elementsSeen, currentPermutation);
currentPermutation.RemoveAt(currentPermutation.Count - 1);
elementsSeen[i] = 0;
}
}
}
}
}