Permutation Algorithmus für Integer-array in Java

Habe ich ein Beispiel generieren, die alle char-Permutationen in einer Zeichenfolge wie folgt:

static ArrayList<String> permutations(String s) {
        if (s == null) {
            return null;
        }

        ArrayList<String> resultList = new ArrayList<String>();

        if (s.length() < 2) {
            resultList.add(s);

            return resultList;
        }

        int length = s.length();
        char currentChar;

        for (int i = 0; i < length; i++) {
            currentChar = s.charAt(i);

            String subString = s.substring(0, i) + s.substring(i + 1);

            ArrayList<String> subPermutations = permutations(subString);

            for (String item : subPermutations) {
                resultList.add(currentChar + item);
            }
        }

        return resultList;
    } 

Ich bin zu versuchen, die gleiche Funktion, aber zurück ArrayList, und um int[] als parameter. Ich Tue dies, rekursiv wie folgt:

static ArrayList<int[]> permutations(int[] arr) {
        ArrayList<int[]> resultList = new ArrayList<int[]>();

        if (arr.length < 2) {
            resultList.add(arr);

            return resultList;
        } 

        for (int i = 0; i < arr.length; i++) {
            int currentItem = arr[i];
            int[] newArr = new int[arr.length - 1];
            int[] newPermutation = new int[arr.length];
            int j;

//         System.arraycopy(arr, 0, newArr, 0, i);
//         System.arraycopy(arr, i + 1, newArr, i, arr.length - i - 1);

            for (j = 0; j < i; j++) {
                newArr[j] = arr[j];
            }

            for (j = i + 1; j < arr.length; j++) {
                newArr[j - 1] = arr[j];
            }

            ArrayList<int[]> subPermutations = permutations(newArr);

            newPermutation[0] = currentItem;

//         for (int i1 = 0; i1 < subPermutations.size(); i1++) {
//             for (j = 0; j < subPermutations.get(i1).length; j++) {
//                 newPermutation[j + 1] = subPermutations.get(i1)[j];
//             }
//             
//             resultList.add(newPermutation);
//         }

            for (int[] item : subPermutations) {
                for (j = 0; j < item.length; j++) {
                    newPermutation[j + 1] = item[j];
                }

                resultList.add(newPermutation);
            }

//         return resultList;
        }

        return resultList;
    }

Beim übergeben von arrays der Größe 0, 1 und 2 als parameter, ist alles in Ordnung. Für alles andere, größer als 2, ich bekomme die richtige Anzahl von Permutationen, aber Sie wiederholen sich. Hier ist das Ergebnis für size == 3, und der übergabe von { 1, 5, 4 }:

1 4 5 
1 4 5 
5 4 1 
5 4 1 
4 5 1 
4 5 1

Bitte geben Sie mir einige Ratschläge, wenn Sie auf diese Fragen vor.

Vielen Dank im Voraus!

Auch, nehmen Sie eine kurze Arrays wie {1,2,3}, und die Arbeit durch Ihren Algorithmus mit Papier und Bleistift.
Das ist die Antwort, nicht tun, was bereits vorhanden ist. stackoverflow.com/a/25704984/5218261

InformationsquelleAutor ppalancica | 2014-01-03

Schreibe einen Kommentar