现在的位置: 首页 > 综合 > 正文

全排列的解法

2018年05月25日 ⁄ 综合 ⁄ 共 508字 ⁄ 字号 评论关闭

    全排列可以用深搜的方式求解。解答树如下:


    可以运行的代码:

import java.util.ArrayList;
import java.util.List;


public class Perm {
	public static Integer[] data = {19, 37, 61, 79, 89};
	public static int depth;
	public static List<Integer> res = new ArrayList<Integer>();
	public static int cnt = 0;
	
	public static void main(String[] args) {
		perm();
		System.out.println(cnt);
	}
	
	public static void perm() {
		if(depth == data.length) {
			System.out.println(res);
			cnt ++;
			return ;
		}
		for(int i=0; i<data.length; i++) 
			if(!res.contains(data[i])){
			res.add(data[i]);
			depth++;
			perm();
			res.remove(res.size() - 1);
			depth--;
		}
		
	}
}


抱歉!评论已关闭.