如何使用重复值打印所有不同的排列

时间:2018-02-24 03:44:26

标签: java duplicates permutation

我最初有一个方法来创建Integer列表的每个排列。我添加了一些代码,不重复相同的排列(如果列表中有重复的项目)。但是,仍然存在一些重复。我不知道如何解决这个问题。这是我的代码:

static void permute(List<Integer> arr, int k) {
    for (int i = k; i < arr.size(); i++) {
            if (i < arr.size() - 1) {
                while (arr.get(i) == arr.get(i + 1)) {
                    if (i < arr.size() - 2) {
                        i++;
                    } else {
                        i++;
                        break;
                    }
                }
            }

        Collections.swap(arr, i, k);
        permute(arr, k + 1);
        Collections.swap(arr, k, i);
    }

    if (k == arr.size() - 1) {
        System.out.println(arr);
    }
}

应该注意的是,该列表先前是按数字顺序排列的。

我的代码的工作原理是将列表中的第一个数字设置为静止,然后将位置一直向下移动到列表中的最后一个项目。然后向后工作它交换并逐步向后移动递归到交换元素。 while语句应跳过重复,跳过过去相同的值,否则将被交换。

示例输入将是置换([11,11,43,61],0)并且它此时会产生输出:

[11, 11, 43, 61]
[11, 11, 61, 43]
[11, 43, 11, 61]
[11, 43, 61, 11]
[11, 61, 43, 11]
[11, 61, 11, 43]
[43, 11, 11, 61]
[43, 11, 61, 11]
[43, 61, 11, 11]
[61, 11, 43, 11]
[61, 11, 11, 43]
[61, 43, 11, 11]
[61, 11, 43, 11]
[61, 11, 11, 43]

[61,11,11,43]和[61,11,43,11]是重复的,不应该存在。

1 个答案:

答案 0 :(得分:0)

//swap integer in array.
public static void swap(int arr[], int i, int j) {
    int t = arr[i];
    arr[i] = arr[j];
    arr[j] = t;
}

// This function finds the index of the smallest character
// which is greater than 'first' and is present in str[l..h]
public static int findCeil(int str[], int first, int l, int h) {
    // initialize index of ceiling element
    int ceilIndex = l;

    // Now iterate through rest of the elements and find
    // the smallest character greater than 'first'
    for (int i = l + 1; i <= h; i++)
        if (str[i] > first && str[i] < str[ceilIndex])
            ceilIndex = i;

    return ceilIndex;
}

// Print all permutations of str in sorted order
public static void permutations(int arr[]) {

    // Get size of string
    int size = arr.length;


    // Sort the string in increasing order
    Arrays.sort(arr);

    // Print permutations one by one
    boolean isFinished = false;

    while (!isFinished) {

        System.out.println(Arrays.toString(arr));

        // Find the rightmost character which is smaller than its next
        // character. Let us call it 'first char'
        int i;
        for (i = size - 2; i >= 0; --i)
            if (arr[i] < arr[i + 1])
                break;

        // If there is no such chracter, all are sorted in decreasing order,
        // means we just printed the last permutation and we are done.
        if (i == -1)
            isFinished = true;
        else {
            // Find the ceil of 'first char' in right of first character.
            // Ceil of a character is the smallest character greater than it
            int ceilIndex = findCeil(arr, arr[i], i + 1, size - 1);

            // Swap first and second characters
            swap(arr, i, ceilIndex);

            // Sort the string on right of 'first char'
            Arrays.sort(arr, i + 1, size);

        }
    }
}