c# - 从包含 m 个项目的集合 S 中选择到长度为 N (N>m) 的另一个列表中的排列

标签 c# arrays algorithm permutation

我已经阅读了一些实现,例如 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?

https://codereview.stackexchange.com/questions/194967/get-all-combinations-of-selecting-k-elements-from-an-n-sized-array?newreg=92ded52aec7b4f9aaf161db14d07ee7a

最佳答案

这样的东西行得通吗?

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/

相关文章:

c# - VB param ( , ) 中的奇怪参数转换为 C#

c# - PowerShell 在读取文件时保持文本格式

c# - SerialPort.Write(byte[], int, int) 的文档是否具有误导性?

javascript - 模拟整数溢出的最佳执行方式?

algorithm - Haskell 上枚举总和和乘积类型的通用算法?

c# - 是否可以在.Net 3.5中运行 Entity Framework 4.1

java - 使用用户输入数组的移动平均线

c - 将输入文件分成两个不同的二维数组

java - 我的代码中需要什么来检查数组中的元素是否与前一个元素相同

java - 定义轮廓是否闭合