假设我有一个2x2阵列。
[1, 2]
[3, 4]
而且我希望所有对都可以。
[1, 2] [1, 3] [1, 4] [2, 3] [2, 4] [3, 4]
而且我也不想像这样的反向对[2, 1]
。
有没有人对这个问题有好的解决方案?
实际上,您将需要三个嵌套循环,或者将2d列表转换为1d列表,然后获得Permutations:
List<List<int>> My2DList = new List<List<int>>() { new List<int>(){ 1, 2 }, new List<int>(){ 3, 4 } }; // your initial 2d list
List<int> My1DList = My2DList.Cast<int>().ToList(); // convert to 1d list
List<List<int>> Permutations = new List<List<int>>(); // prepare a container
for (int i = 0; i < My1DList.Count; i++)
for(int j = i; j < My1DList.Count; j++)
Permutations.Add(new List<int>() { My1DList[i], My1DList[j] }); // add your permutations
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句