我想获得排列的特定组合吗? [英] I want to get a specific combination of permutation?

查看:103
本文介绍了我想获得排列的特定组合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得像字母这样的字符串排列的特定组合。为了理解我,我将向您展示我使用的代码:

I want to get specific combination of permutation of string like alphabet. To understand me, I'll show you the code that I using:

public class PermutationExample {

public static List<String> getPermutation(String input) {

    List<String> collection = null;

    if (input.length() == 1) {
        collection = new ArrayList<String>();
        collection.add(input);
        return collection;
    } else {
        collection = getPermutation(input.substring(1));
        Character first = input.charAt(0);
        List<String> result = new ArrayList<String>();
        for (String str : collection) {
            for (int i = 0; i < str.length(); i++) {
                String item = str.substring(0, i) + first
                        + str.substring(i);
                result.add(item);
            }
            String item = str.concat(first.toString());
            result.add(item);
        }
        return result;
    }

}

public static void main(String[] args) {
    System.out.println(PermutationExample.getPermutation("ABCD"));

}
}

此代码效果很好,我可以得到每个组合,我可以从列表中获取,如果我需要第5个元素,则可以接收。但是,如果字符串是字母...,则不起作用,则太大。我要做的是从所有26个中获取像1221这样的特定元素!组合?

This code works well and i can get every combination, I can take it from the list, if I need 5-th element, I can receive it. But if the string is the alphabet ... , didn't works, it's too big. What I have to do, to get the specific element like 1221-th from all 26! combinations ?

推荐答案

我前一段时间解决了一个类似的问题,仅在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.

这篇关于我想获得排列的特定组合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆