简体   繁体   English

使用递归在Java中打印长度为N的位串的排列

[英]Using recursion to print permutations of a bit string of length N in Java

I'm trying to obtain a linked list of integer arrays representing the possible permutations of a bit string of length N eg for N = 2 我试图获得一个整数数组的链表,表示长度为N的位串的可能排列,例如N = 2

00 01 10 11 00 01 10 11

I successfully wrote the code representing the bits as strings like so: 我成功地将表示代码的代码写为字符串,如下所示:

public static LinkedList<String> printBin(String soFar, int iterations) {
    if(iterations == 0) {
        LinkedList<String> ret = new LinkedList<String>();
        ret.add(soFar);
        return ret;
    }else {
        LinkedList<String> ret = new LinkedList<String>();
        ret.addAll(printBin(soFar + "0", iterations - 1));
        ret.addAll(printBin(soFar + "1", iterations - 1));
        return ret;
    }
}

However i tried to convert this code to work with arrays of integers and as a result for N = 2 I get 但是我试图将此代码转换为使用整数数组,因此N = 2我得到了

11 11 11 11 11 11 11 11

SSCE below: SSCE如下:

public class Main {
    public static void main(String[] args){
        int numberOfBits = 2;
        LinkedList<int []> results = printBin(new int[numberOfBits], 0, numberOfBits);
        Iterator<int[]> i = results.iterator();
        while(i.hasNext()){
            int[] temp = i.next();
            for(int j = 0; j < temp.length; j++){
                System.out.print(temp[j]);
            }
            System.out.println("");
        }
    }


    public static LinkedList<int[]> printBin(int[] soFar, int counter, int iterations) {
        if(iterations == 0) {
            LinkedList<int[]> ret = new LinkedList<int[]>();
            ret.add(soFar);
            return ret;
        }else {
            LinkedList<int[]> ret = new LinkedList<int[]>();
            int[] soFar1 = soFar; soFar1[counter] = 0;
            int[] soFar2 = soFar; soFar2[counter] = 1;
            ret.addAll(printBin(soFar1, counter + 1, iterations - 1));
            ret.addAll(printBin(soFar2, counter + 1, iterations - 1));
            return ret;
    }
}

} }

Any help would be greatly appreciated. 任何帮助将不胜感激。

you cannot do 你不能这样做

    int[] soFar1 = soFar; soFar1[counter] = 0;
    int[] soFar2 = soFar; soFar2[counter] = 1;

It will cause sofar1 and sofar2 to point to the same array. 它将导致sofar1和sofar2指向同一个数组。 you need to make a new array and copy the stuff to it. 你需要创建一个新的数组并将其复制到它。

int[] soFar1=(int[])soFar.clone(); soFar1[counter]=0;
int[] soFar2=(int[])soFar.clone(); soFar2[counter]=1;

The problem with your code is that it keeps reusing the same instance of the array soFar on each level of invocation, rather than making a copy. 您的代码的问题在于它在每个调用级别上不断重用数组soFar的相同实例,而不是复制。 If you change the code to 如果您将代码更改为

int[] soFar1 = (int[])soFar.clone(); soFar1[counter] = 0;
int[] soFar2 = (int[])soFar.clone(); soFar2[counter] = 1;

your code should produce the results that you want ( demo on ideone ). 你的代码应该产生你想要的结果( 在ideone上演示 )。

However, you would be better off iterating all integers from 0 to (1 << numberOfBits) - 1 , and printing their binary representations: 但是,最好将所有整数从0迭代到(1 << numberOfBits) - 1 ,然后打印它们的二进制表示:

LinkedList<int[]> ret = new LinkedList<int[]>();
int endMask = 1 << numberOfBits;
for (int mask = 0 ; mask != endMask ; mask++) {
    int[] combo = new int[numberOfBits];
    for (int i = 0 ; i != numberOfBits ; i++) {
        combo[i] = ((mask & (1 << i)) != 0) ? 1 : 0;
    }
    ret.add(combo);
}

Here is a demo of iterative method on ideone . 这是一个关于ideone的迭代方法演示

Suggestion: Don't work with an array in the parameters, work with the return values, bottom up 建议:不要在参数中使用数组,使用返回值,自下而上

public static LinkedList<LinkedList<int>> printBin( int bits) {
    if(bits == 0) {
        LinkedList<LinkedList<int>> ret = new LinkedList<LinkedList<int>>();
        ret.add(new LinkedList<Int>());
        ret.get(0).add(0);
        ret.get(0).add(1);
        return ret;
    }else {
        LinkedList<LinkedList<int>> ret = printBin(bits-1);
        LinkedList<LinkedList<int>> ret2 = printBin(bits-1); // this should be deep copied
        for(LinkedList<int> bitstring : ret){
            bitstring.add(0); 
        }
        for(LinkedList<int> bitstring : ret2){
            bitstring.add(1); 
        }

        ret.addAll(ret2);
        return ret;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM