求一个全排列函数

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);
	}
}


你可能感兴趣的:(算法)