且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我想获得排列的特定组合吗?

更新时间:2023-08-18 09:13:04

我前一段时间解决了一个类似的问题,仅在python中。

I solved a similar problem a while ago, only in python.

如果您需要的只是第n个排列,那么您可以做得更好,然后生成每个排列并返回第n个,如果您想仅生成所需的排列。

If what you need is simply the n-th permutation, then you can do a lot better then generating every permutation and returning the n-th, if you try to think about generating only the permutation you need.

您可以简单地做到这一点,方法是弄清所需排列数前面的元素是什么,然后

You can do this "simply" by figuring out what should be the element in front for the number of permutations you want, and then what should be the remaining of the elements recursively.

假设任何值的集合[0,...,X]等于col [n]< ; col [n + 1]
对于N个元素,有N个!

Assume a collection of values [0, ... ,X], for any values such that col[n] < col[n+1] For N elements, there are N! possible permutations, the case when the collection will be perfectly reversed.

我们将在每个(N-1)之后看到集合头部的变化!排列,所以如果n < (N-1)!,头就是头。这样便剩下了一些排列,您可以递归应用相同的逻辑。

We will see the change in the head of the collection after each (N-1)! permutations, so if n < (N-1)!, the head is the head. You then have a remaining number of permutations, and you can apply the same logic recursively.

这有帮助吗?我知道这是一个相当高的水平,您需要考虑一下,但是也许它会让您走上正确的轨道。

Does this help? I know it's fairly high level and you'll have to think a bit about it, but maybe it'll get you on the right track.