`

Java打印全排列

 
阅读更多
	public static void permute(char[] str, int start) {
		int length = str.length - 1;
		if (start > length) {
			System.out.println(str);
		} else {
			for (int i = start; i <= length; i++) {
				char[] temp = str.clone(); // temp will be str
				temp[i] = str[start]; // with i and low
				temp[start] = str[i]; // swapped
				permute(temp, start + 1);
			}
		}
	}
        permute(new char[]{'a','b','c'},0);


这个好理解一些
	public void printAll(List candidate, String prefix) {
		if (candidate.size() == 1) {
			System.out.println(prefix + candidate.get(0));
		} else {
			for (int i = 0; i < candidate.size(); i++) {
				List temp = new LinkedList(candidate);
				printAll(temp, prefix + temp.remove(i));// 递归调用
			}
		}
	}
        printAll(list,"");
        
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics