我已经阅读了一些实现,例如 What is the best way to find all combinations of items in an array?
他们的实现的好处是很多都是泛型的,而不仅仅是 int 数组(或者更糟的是,只有正整数数组)
但是我找不到可以接受大小为 m 的数组(名称数组“S”)的项目的东西。从数组“S”中取出项目,将它们放入另一个大小为 n 的数组“P”(n 小于 m,我不理解的常见限制)。
例如,
S = [-1, 1]
P[j] = [1,1,1,1], [1, -1, 1, 1], [1, -1, -1, 1], [1, -1, -1, -1], [-1, -1, -1, -1], ... [-1, 1, 1, -1], [1, -1, -1, 1], [-1, 1, -1, 1], [1, -1, 1, -1]
j = permutations = 0 ... pow(m,n), in this example pow(2, 4) = 16
请问有什么 C# 或 Python 语言吗?
另外,时间复杂度...
引用资料:
What is the best way to find all combinations of items in an array?
最佳答案
这样的东西行得通吗?
def f(ms, k):
stack = [[m] for m in ms]
while stack:
next = stack.pop()
if len(next) == k:
yield next
else:
stack.extend([(next[:] + [m]) for m in ms])
print([comb for comb in f([-1, 1], 4)])
关于c# - 从包含 m 个项目的集合 S 中选择到长度为 N (N>m) 的另一个列表中的排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58189120/