Java 全排列

2019-03-18 Yazzyk

学习视频:bilibilip2

代码

public class 递归全排列 {
    public static void main(String[] args) {
		// TODO 自动生成的方法存根
		char[] data = {'A','B','C'};
		fun(data, 0);
	}

  public static void fun(char[] data,int n) {
    if(n == data.length) {
      for(int i=0;i<data.length;i++) {
        System.out.print(data[i]+" ");
      }
      System.out.println();
    }
  
      for(int i=n;i<data.length;i++) {
      //试探  
      char t = data[n];  
      data[n] = data[i];  
      data[i] = t;  

      fun(data, n+1);
      //回溯  
      t = data[n];  
      data[n] = data[i];
      data[i] = t;
    }
  }
}

output

A B C 
A C B 
B A C 
B C A 
C B A 
C A B