分治法实现1-N的数字按字典序全排列组合 Java语言

package 分治法;


import java.util.Arrays;
/*
 * 将数字 1 - n进行全排列  按字典序从小到大输出
 * 如  1 - 3  
 * 	123 132 213 231 312 321
 */
class GenerateP{
	
	private int n;  //  求 1-n所有数字的全排列
	private final int maxn = 110;//最多可排列组合的长度  1-100
	private boolean [] hashTable;
	private int [] p;
	
	public GenerateP(int n) {
		// TODO Auto-generated constructor stub
		this.n = n;
		hashTable = new boolean[maxn];
		p = new int [maxn];
		Arrays.fill(hashTable, false);
		Arrays.fill(p, 0);
	}
	public void generatep(int index){
		if(index == n + 1){
			for(int i = 1; i <= n; i++){
				System.out.print(p[i]);
			}
			System.out.println();
			return;
		}
		
		for(int x = 1; x <= n; x++){
			if(hashTable[x] == false){
				p[index] = x;
 				hashTable[x] = true;
				generatep(index + 1);
				hashTable[x] = false;
			}
		}
	}
}
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GenerateP generateP = new GenerateP(3);
		generateP.generatep(1);

	}

}

  

原文地址:https://www.cnblogs.com/mlsq2015/p/5898919.html