-3

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;
            }
        }
     }
  }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
FrostyStraw
  • 1,628
  • 3
  • 25
  • 34

1 Answers1

4

You add the same List over and over to the result. In the end you have n! pointers to the same, empty List (currentPermutation is empty in the end). All you need is to make a copy of the List for every permutation and add the new List to the result.

list.Add(currentPermutation.ToList()); //clone the list

"calling ToList() on a List" may seem a bit tricky, you are actually calling ToList() on IEnumerable, is that better? The point is to create new, independent List.

Maybe you do not exactly understand what List in C# is. List is an object. If you add it somewhere, you do not add it literally there, you just add pointer to this object.

Antonín Lejsek
  • 6,003
  • 2
  • 16
  • 18
  • Okay..changing that worked, but I don't really understand how ToList() fixes it..I'm calling ToList() on a List? What does that even mean? – FrostyStraw Nov 21 '17 at 04:28
  • @FrostyStraw I added some more explanation, hope it helps. – Antonín Lejsek Nov 21 '17 at 04:48
  • ToList is a Linq extension menthod, just to be even more clear. Beyond that, this whole data structure seems incredibly cumbersome to my tired brain. – theMayer Nov 21 '17 at 04:51
  • @AntonínLejsek Okay, I can more clearly see how I'm just adding the pointer to the same list 6 times. And the fact that I am removing the last element from the list each time I'm backtracking also makes sense as to why it's an empty list each time. Then calling ToList() just returns a new list and that's what is passed int other Add method of the list of lists..is this correct? – FrostyStraw Nov 21 '17 at 05:08