import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> permuteUnique (int[] num) { // write code here Arrays.sort(num); ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>(); boolean[] arr = new boolean[num.length]; ArrayList<Integer> tmp = new ArrayList<Integer>(); recursion(ret, num, tmp, arr); return ret; } public void recursion(ArrayList<ArrayList<Integer>> ret, int[] num, ArrayList<Integer> tmp, boolean[] arr) { if (num.length == tmp.size()) { ret.add(new ArrayList<Integer>(tmp)); return; } for (int i = 0; i < num.length; i++) { if (arr[i]) { continue; } if (i > 0 && num[i] == num[i-1] && !arr[i-1]) { continue; } arr[i] = true; tmp.add(num[i]); recursion(ret, num, tmp, arr); arr[i] = false; tmp.remove(tmp.size() - 1); } } }