package Level3;
import java.util.ArrayList;
/**
* Permutations
*
* 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].
*
*/
public class S46 {
public static void main(String[] args) {
int[] S = {1,2,3};
System.out.println(permute(S));
}
public static ArrayList> permute(int[] S) {
ArrayList> ret = new ArrayList>();
ArrayList list = new ArrayList();
rec(S, ret, list);
return ret;
}
public static void rec(int[] S, ArrayList
> ret, ArrayList list){
// 当数组长度为0时,添加入ret
if(S.length == 0){
ret.add(new ArrayList(list)); // 必须基于现场新建一个ArrayList然后添加入ret!
return;
}
// 遍历数组中的每一个数作为第一个元素
for(int i=0; i