数组的全排列(JAVA实现)

input:[1, 2, 3]

output: [ [1,2,3], [1,3,2], [2,1,3,], [2,3,1], [3,1,2], [3,2,1] ]

    public static void sort(int[] a, int b, int e) {
        if(b==e) {
            System.out.println(Arrays.toString(a));
        } else {
            for(int i=b; i<=e; i++) {
                swap(a, b, i);
                sort(a, b+1, e); //recursive
                swap(a, b, i);
            }
        }
    }
    
    public static void swap(int[] arr, int a, int b) {
        int t = arr[a];
        arr[a] = arr[b];
        arr[b] = t;
    }        

猜你喜欢

转载自www.cnblogs.com/macyzhang/p/9860064.html
今日推荐