C# Permutation of an array of arraylists?

后端 未结 13 939
难免孤独
难免孤独 2020-11-27 18:10

I have an ArrayList[] myList and I am trying to create a list of all the permutations of the values in the arrays.

EXAMPLE: (all values are strings)

         


        
13条回答
  •  天命终不由人
    2020-11-27 18:56

    Here is a version which uses very little code, and is entirely declarative

        public static IEnumerable> GetPermutations(IEnumerable collection) where T : IComparable
        {
            if (!collection.Any())
            {
                return new List>() {Enumerable.Empty() };
            }
            var sequence = collection.OrderBy(s => s).ToArray();
            return sequence.SelectMany(s => GetPermutations(sequence.Where(s2 => !s2.Equals(s))).Select(sq => (new T[] {s}).Concat(sq)));
        }
    

提交回复
热议问题