描述
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]
代码
package com.lilei.myes.es.pack1107; public class quanpailie { public static void main(String[] args) {
char[] cs = new char[] { 'a', 'b', 'c','d' }; pailie(cs, 0); } public static void pailie(char[] cs, int e) { if (e == cs.length) {
System.out.println(new String(cs));
} else { for (int i = e; i < cs.length; i++) {
swap(cs, i, e);
pailie(cs, e + 1);
swap(cs, i, e); } }
} static void swap(char[] cs, int a, int b) {
char tmp = cs[a];
cs[a] = cs[b];
cs[b] = tmp;
} }