對List中元素子集合進行組合排列,使用了遞歸,代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { CombinationsPermutations("asdf", 3); } private static void RotateLeft<T>(IList<T> sequence, int start, int count) { T tmp = sequence[start]; sequence.RemoveAt(start); sequence.Insert(start + count - 1, tmp); } public static IEnumerable<IList<T>> Combinations<T>( IList<T> sequence, int start, int count, int choose) { if (choose == 0) yield return sequence; else { for (int i = 0; i < count; i++) { foreach (var perm in Combinations( sequence, start + 1, count - 1 - i, choose - 1)) yield return perm; RotateLeft(sequence, start, count); } }