求一個全排列函數

px; ">求一個全排列函數:
如 p([1,2,3])輸出:

[123]、 [132]、 [213]、 [231]、 [321]、 [323]

public class ComposeArr {
	public static void solution(int[] a, int start, int end) {
		if (null == a) {
			return;
		}
		if (start > end) {
			return;
		}
		if (start == end) {
			for (int i = 0; i < a.length; i++) {
				System.out.print(a[i] + " ");
			}
			System.out.println();
		} else {

			for (int i = start; i <= end; i++) {
				int tmp = a[start];
				a[start] = a[i];
				a[i] = tmp;
				solution(a, start + 1, end);
				int tmp1 = a[start];
				a[start] = a[i];
				a[i] = tmp1;
			}

		}
	}

	public static void main(String[] args) {
		int[] a = { 1, 2, 3, 4, 5 };
		solution(a, 0, 4);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章