Permutations @LeetCode
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<ArrayList<Integer>> permute(int[] S) { ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list = new ArrayList<Integer>(); rec(S, ret, list); return ret; } public static void rec(int[] S, ArrayList<ArrayList<Integer>> ret, ArrayList<Integer> list){ // 当数组长度为0时,添加入ret if(S.length == 0){ ret.add(new ArrayList<Integer>(list)); // 必须基于现场新建一个ArrayList然后添加入ret! return; } // 遍历数组中的每一个数作为第一个元素 for(int i=0; i<S.length; i++){ // 构建一个新子数组来递归 int[] sub = new int[S.length-1]; System.arraycopy(S, 0, sub, 0, i); System.arraycopy(S, i+1, sub, i, S.length-i-1); // System.out.println(Arrays.toString(sub)); list.add(S[i]); rec(sub, ret, list); list.remove(list.size()-1); // 恢复现场 } } }
补充:软件开发 , Java ,