简体   繁体   中英

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

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

11 11 11 11

SSCE below:

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. 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. 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 ).

However, you would be better off iterating all integers from 0 to (1 << numberOfBits) - 1 , and printing their binary representations:

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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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