1 public class Solution { 2 public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = num.length; 6 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); 7 HashSet<ArrayList<Integer>> tmpResult = new HashSet<ArrayList<Integer>>(); 8 permutation(num, 0, len, result, tmpResult); 9 return result; 10 } 11 12 public void permutation(int[] num, int depth, int len, ArrayList<ArrayList<Integer>> result, HashSet<ArrayList<Integer>> tmpResult){ 13 14 if(depth == len){ 15 ArrayList<Integer> per = new ArrayList<Integer>(); 16 for(int i =0 ; i < len; i++) 17 per.add(num[i]); 18 19 if(tmpResult.add(per)) 20 result.add(per); 21 } 22 23 for(int i = depth; i < len; i++) { 24 if(i != depth && num[i] == num[depth]) 25 continue; 26 27 int tmp = num[i]; 28 num[i] = num[depth]; 29 num[depth] = tmp; 30 31 permutation(num, depth + 1, len, result, tmpResult); 32 33 tmp = num[i]; 34 num[i] = num[depth]; 35 num[depth] = tmp; 36 } 37 } 38 }