0

我正在尝试在 Java 中实现“堆算法”(wiki),它构造了给定集合的所有排列。(我知道这在技术上不是 Heap 的算法,因为这里指出了一些微妙之处,但这对我来说目前并不是非常重要)。

我所拥有的:我从一段有效的代码开始,做我想做的事:

    public static <T> List<T[]> Permutations(T a[]) {
        LinkedList<T[]> list = new LinkedList<T[]>();
        heapPermutation(a, a.length, a.length, list);
        return list;
    }

    //Generating permutation using Heap Algorithm 
    public static <T> void heapPermutation(T a[], int size, int n, List<T[]> list) 
    { 
        // if size becomes 1 then adds the obtained 
        // permutation to the list
        if (size == 1) 
            list.add(a.clone());

        for (int i=0; i<size; i++) 
        { 
            heapPermutation(a, size-1, n, list); 

            // if size is odd, swap first and last 
            // element 
            if (size % 2 == 1) 
            { 
                T temp = a[0]; 
                a[0] = a[size-1]; 
                a[size-1] = temp; 
            } 

            // If size is even, swap ith and last 
            // element 
            else
            { 
                T temp = a[i]; 
                a[i] = a[size-1]; 
                a[size-1] = temp; 
            } 
        } 
    } 

    public static void main(String[] args) {
        Character[] chars = new Character[] {'a','b','c','d'};

        List<Character[]> list = Permutations(chars);
        for(Iterator<Character[]> i = list.iterator(); i.hasNext();) {
            Character[] array = i.next();
            for(int j = 0; j < array.length; j++) {
                System.out.print(array[j] + " ");
            }
            System.out.println();
        }
    }

再说一遍:这段代码可以正常工作并输出我想要的东西。

良好的输出:

a b c d 
b a c d 
c a b d 
a c b d 
b c a d 
c b a d 
d b c a 
b d c a 
c d b a 
d c b a 
b c d a 
c b d a 
d a c b 
a d c b 
c d a b 
d c a b 
a c d b 
c a d b 
d a b c 
a d b c 
b d a c 
d b a c 
a b d c 
b a d c 

我想要什么:我想复制上面的代码,但是用Lists(或ArrayLists、LinkedLists 等)替换所有数组实例。

我尝试过的:这是我尝试过的修改:

   public static <T> List<List<T>> Permutations(List<T> a) {
        List<List<T>> list = new ArrayList<List<T>>();
        heapPermutation(a, a.size(), a.size(), list);
        return list;
    }

    //Generating permutation using Heap Algorithm 
    public static <T> void heapPermutation(List<T> a, int size, int n, List<List<T>> list) 
    { 
        List<T> aTemp = new ArrayList<T>(a);

        // if size becomes 1 then adds the obtained 
        // permutation to the list
        if (size == 1) {
            list.add(aTemp);
        }

        for (int i=0; i<size; i++) 
        { 
            heapPermutation(aTemp, size-1, n, list); 

            // if size is odd, swap first and last 
            // element 
            if (size % 2 == 1) 
            { 
                T temp = aTemp.get(0); 
                aTemp.set(0, a.get(size-1)); 
                aTemp.set(size-1,temp); 
            } 

            // If size is even, swap ith and last 
            // element 
            else
            { 
                T temp = aTemp.get(0); 
                aTemp.set(i, a.get(size-1));
                aTemp.set(size-1, temp);
            } 
        } 
    } 

    public static void main(String[] args) {
        List<Character> list = new ArrayList<Character>();
        list.add('a'); list.add('b'); list.add('c'); list.add('d');
        System.out.println(Permutations(list));
    }

但是,与第一个代码块不同,这并没有给出我想要的:

错误输出:

[[a, b, c, d], [b, a, c, d], [c, b, a, d], [b, c, a, d], [c, b, c, d], [b, c, c, d], [d, b, c, a], [b, d, c, a], [c, b, d, a], [b, c, d, a], [c, b, c, a], [b, c, c, a], [d, d, c, d], [d, d, c, d], [c, d, d, d], [d, c, d, d], [c, d, c, d], [d, c, c, d], [d, d, d, d], [d, d, d, d], [d, d, d, d], [d, d, d, d], [d, d, d, d], [d, d, d, d]]

第二个代码块中发生了什么使其不正确?我 100% 确定这是由于我对ListJava 中 s 的一些微妙之处缺乏了解,但我不知道罪魁祸首是什么。

在问这里之前,我尝试了添加的第二个代码块List<T> aTemp = new ArrayList<T>(a);,并且我还尝试修改它的各个部分以更改 to 的实例,a反之亦然aTemp。我试过的都没有用。

编辑 1

在下面的评论中,用户@GPI 在我的情况下指出了一个错字。更正T temp = aTemp.get(0);T temp = aTemp.get(i);,我有以下输出:

[[a, b, c, d], [b, a, c, d], [c, b, a, d], [b, c, a, d], [c, b, c, d], [b, c, c, d], [d, b, c, a], [b, d, c, a], [c, b, d, a], [b, c, d, a], [c, b, c, a], [b, c, c, a], [d, d, c, b], [d, d, c, b], [c, d, d, b], [d, c, d, b], [c, d, c, b], [d, c, c, b], [d, d, d, c], [d, d, d, c], [d, d, d, c], [d, d, d, c], [d, d, d, c], [d, d, d, c]]

请注意,这也是不正确的,因为它包含许多实际上不是排列的重复项/列表(例如[d, d, d, c])。

4

1 回答 1

2

 一、笔误

如前所述,要解决的第一件事是您的拼写错误:偶数大小写应为T temp = aTemp.get(i);

二、克隆

问题的症结在于您没有掌握如何/何时修改列表/数组,以及何时将其复制到累积。

甚至不用看你的方法在做什么,我们就可以看到数组版本在适当的位置操作数组,除非它的大小为 1,在这种情况下它被复制到结果列表中。

换句话说,在数组版本中,它始终是交换其项目的同一个数组,无论递归有多深。只有当我们想记住项目的某个排列时,我们才会复制它,以确保排列被冻结a.clone()

另一方面,列表版本在每次启动时都会复制其输入。换句话说,在每个递归阶段,原始列表的本地副本用于交换项目。当递归展开时,此副本的当前项目排列将丢失。

因此,如果您删除列表的克隆,仅将其保留在if size == 1)机箱内,则应该没问题。

最后,并不是列表案例与数组之间存在细微差别,只是您在没有分析影响的情况下移动了一些“克隆”逻辑。

有了它,输出变为:

[[a, b, c, d], 
[b, a, c, d],
[c, a, b, d],
[a, c, b, d],
[b, c, a, d],
[c, b, a, d],
[d, b, c, a],
[b, d, c, a],
[c, d, b, a],
[d, c, b, a],
[b, c, d, a],
[c, b, d, a],
[d, a, c, b],
[a, d, c, b],
[c, d, a, b],
[d, c, a, b],
[a, c, d, b],
[c, a, d, b],
[d, a, b, c],
[a, d, b, c],
[b, d, a, c],
[d, b, a, c], 
[a, b, d, c],
[b, a, d, c]]
于 2020-01-02T13:36:03.517 回答