相关疑难解决方法(0)

从n返回k个元素的所有组合的算法

我想写一个函数,它将一个字母数组作为参数,并选择一些字母.

假设您提供了8个字母的数组,并希望从中选择3个字母.然后你应该得到:

8! / ((8 - 3)! * 3!) = 56
Run Code Online (Sandbox Code Playgroud)

数组(或单词)返回,每个包含3个字母.

algorithm combinations

551
推荐指数
23
解决办法
43万
查看次数

如何在F#中使用可变列表?

我是F#的新手,我正在编写一个程序,需要找到某个列表中给定长度的每个子列表.我不知道如何解决这个问题所以我读了这个问题并决定将答案移植到F#.这就是我所拥有的:

let rec getSubLists (len : int) (list : List<int>) : List<List<int>> =
  let result = new List<List<int>>()
  let current = new List<int>()

  let rec findSubLists (len : int) (superSet : List<int>) (current : List<int>) (soln : List<List<int>>) (idx : int) : unit =
    if current.Length = len then soln.Insert(len - 1, current)
    elif idx = superSet.Length then
      let x = superSet.[idx] 
      current.Insert(len, x)
      findSubLists len superSet current soln (idx + 1)
      current.RemoveAt(x)
      findSubLists len superSet …
Run Code Online (Sandbox Code Playgroud)

f# list mutable

2
推荐指数
1
解决办法
84
查看次数

集/数组中的每个(特定大小的)组合,没有重复的项目

假设我设置了[1、2、3、4、5、6、7]

我想要以下内容作为回报[1、2、3、4、5] [4、3、2、1、6] [7、5、1、3、2] ......... 。

本质上,正如标题所述,我希望从数组中生成特定大小的组合,但每个组合都不能有任何重复的项(因此,如果您有这个主意,则没有aaab,aaac)。

我在这里也找到了另一个问题,但在组合中却存在重复。我试图调整并编写递归函数无济于事:/

php arrays combinations permutation

0
推荐指数
1
解决办法
431
查看次数

标签 统计

combinations ×2

algorithm ×1

arrays ×1

f# ×1

list ×1

mutable ×1

permutation ×1

php ×1