All numbers

All numbers

Problem Description

Write a program for a non-repeating array of numbers to output the full array.

For example, given an array:

[1, 2, 3]

output:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]

Solutions

This problem is very classic, and then try to use the idea of ​​mathematical induction to solve this problem.

In middle school, we know that a sequence of length n has n! permutations. Because the first number has n cases, the second number has n-1 cases, and the third number has n-2 cases... There is only one case for the nth number, and the formula is n*( n-1)*(n-2).….*1 = n!

Let's think about it in a different way. Taking the array [1,2,3] as an example, its full arrangement is:

  • The full permutation of the other two numbers whose first number is 1 + the full permutation of the other two numbers whose first number is 2 + the full permutation of the other two numbers whose first number is 3.

  • So how to calculate the full arrangement of two numbers, taking [1,2] as an example, it is:

The full permutation of the remaining numbers whose first digit is 1 + the full permutation of the remaining numbers whose first digit is 2.

  • Because there is only one number left, there is no need to continue, and it can be output at this point.

And so on to the full permutation of n numbers:

  • Let the array p = {r1, r2, r3, r4, r5…., rn}, let the full permutation of p be perm(p), and let pn = p - {rn}.
  • 那么perm(p) = { r1, perm(p1) } + { r2, perm(p2) } + {r3, perm(p3) } + …… + {rn, perm(pn) }。
  • In the same way, you can also calculate perm(p1), perm(p2), perm(p3)...perm(pn).
  • Continue, you can use the recursive solution, the exit of the recursion is that there is only one value in the fully permuted array obtained by perm.

Code

The following is the implementation code in java:

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[] arr = {1,2,3};
        Test t = new Test();
        t.perm(arr, 0, arr.length);
    }
    
    //求数组全排列
    public void perm(int[] nums, int start, int len) {
        //判断递归出口,当start == len - 1时,也就是要做的全排列只有一个值 ,那么就可以输出了
        if(start == len - 1) {
            System.out.println(Arrays.toString(nums));
        }else {
            /*
             * 没有到递归出口时,这一段代码是关键
             * for循环模拟的是:
             * { r1, perm(p1) } + { r2, perm(p2) } + {r3, perm(p3) } + …… + {rn, perm(pn) }
             * 从r1, r2, r3 一直到 rn 作为第一位,求剩下的全排列
             */
            for(int i = start; i < len; i++) {
                swap(nums, start, i);//通过交换,依次将每个数放在第一位
                perm(nums, start + 1, len);//递归调用
                swap(nums, start, i);//交换回来,保证原数组不会变,以进行下一轮全排列
            }
        }
    }
    
    //交换数组中的两个值
    public void swap(int[] nums, int i, int j) {
        int tem = nums[i];
        nums[i] = nums[j];
        nums[j] = tem;
    }
}

Output result:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

refer to:

http://www.cnblogs.com/nokiaguy/archive/2008/05/11/1191914.html

https://blog.csdn.net/randyjiawenjie/article/details/6313729

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325693203&siteId=291194637