我尝试解决下一个练习:
输入:带有count >= 1;
个正整数k
输出: 该整数的所有可能的元组,其长度为k
;
例如
输入:{1, 2}; k = 4
输出:
{
{1, 1, 1, 1},
{1, 1, 1, 2},
{1, 1, 2, 1},
{1, 1, 2, 2},
{1, 2, 1, 1},
{1, 2, 1, 2},
{1, 2, 2, 1},
{1, 2, 2, 2},
{2, 1, 1, 1},
{2, 1, 1, 2},
{2, 1, 2, 1},
{2, 1, 2, 2},
{2, 2, 1, 1},
{2, 2, 1, 2},
{2, 2, 2, 1},
{2, 2, 2, 2}
}
我试图创建一个包含k
输入列表副本,然后使用Combinations
的 array :
public static IEnumerable<IEnumerable<T>> Combinations<T>(
this IEnumerable<T> elements,
int k)
{
return k == 0
? new[] { new T[0] }
: elements.SelectMany((e, i) => elements
.Skip(i + 1)
.Combinations(k - 1)
.Select(c => (new[] { e }).Concat(c)));
}
但是k > 9
花费的时间太长了。是否有一种算法可以在短时间内解决此问题?
答案 0 :(得分:5)
让我们摆脱递归并拥有512
项:
代码:
//TODO: you may want to declare it as IEnumerable<T[]> Combinations<T>
public static IEnumerable<IEnumerable<T>> Combinations<T>(
this IEnumerable<T> elements, int k) {
if (null == elements)
throw new ArgumentNullException(nameof(elements));
else if (k < 0)
throw new ArgumentOutOfRangeException(nameof(k));
T[] alphabet = elements.ToArray();
// Special cases
if (alphabet.Length <= 0)
yield break;
else if (k == 0)
yield break;
int[] indexes = new int[k];
do {
yield return indexes
.Select(i => alphabet[i])
.ToArray();
for (int i = indexes.Length - 1; i >= 0; --i)
if (indexes[i] >= alphabet.Length - 1)
indexes[i] = 0;
else {
indexes[i] += 1;
break;
}
}
while (!indexes.All(index => index == 0));
}
演示:
string report = string.Join(Environment.NewLine, Combinations(new int[] { 1, 2}, 9)
.Select(line => string.Join(", ", line)));
Console.Write(report);
结果:({512
条记录)
1, 1, 1, 1, 1, 1, 1, 1, 1
1, 1, 1, 1, 1, 1, 1, 1, 2
1, 1, 1, 1, 1, 1, 1, 2, 1
1, 1, 1, 1, 1, 1, 1, 2, 2
1, 1, 1, 1, 1, 1, 2, 1, 1
1, 1, 1, 1, 1, 1, 2, 1, 2
1, 1, 1, 1, 1, 1, 2, 2, 1
1, 1, 1, 1, 1, 1, 2, 2, 2
1, 1, 1, 1, 1, 2, 1, 1, 1
...
2, 2, 2, 2, 2, 2, 1, 2, 1
2, 2, 2, 2, 2, 2, 1, 2, 2
2, 2, 2, 2, 2, 2, 2, 1, 1
2, 2, 2, 2, 2, 2, 2, 1, 2
2, 2, 2, 2, 2, 2, 2, 2, 1
2, 2, 2, 2, 2, 2, 2, 2, 2
让我们生成所有2**20 == 1048576
个项目(k = 20
),即生成超过{em> 1百万个大小为20
的数组:
Stopwatch sw = new Stopwatch();
sw.Start();
int count = Combinations(new int[] { 1, 2 }, 20).Count();
sw.Stop();
Console.Write($"{count.ToString()} items at {sw.ElapsedMilliseconds:f0} milliseconds");
结果:
1048576 items at 469 milliseconds