标签:lse display tco href rip img false ems 图片
一.题源
https://www.lintcode.com/problem/permutations-ii/description
https://leetcode-cn.com/problems/permutations-ii/
二.代码分析
1 public class Solution { 2 public static void main(String[] args) { 3 int[] arr = new int[]{1,2,2}; 4 Arrays.sort(arr); 5 ArrayUtils.displayArrayList(permuteUnique(arr)); 6 } 7 8 public static List<List<Integer>> permuteUnique(int[] nums) { 9 List<List<Integer>> result = new ArrayList<>(); 10 boolean[] usedTable = new boolean[nums.length]; 11 backTrace(result,new ArrayList<>(),nums,usedTable); 12 13 return result; 14 } 15 16 private static void backTrace(List<List<Integer>> result,List<Integer> list,int[] nums,boolean[] used){ 17 if(list.size() == nums.length){ 18 result.add(new ArrayList<>(list)); 19 return; 20 } 21 22 for (int i = 0; i < nums.length; i++) { 23 if(used[i]){ 24 continue; 25 } 26 if( i>=1 && !used[i-1] && !used[i] && nums[i]==nums[i-1]){ 27 continue; 28 } 29 list.add(nums[i]); 30 used[i] = true; 31 backTrace(result,list,nums,used); 32 list.remove(list.size()-1); 33 used[i] = false; 34 } 35 } 36 }
三.图解分析
四.总结
注意事项:关键点在于设置好过滤条件
标签:lse display tco href rip img false ems 图片
原文地址:https://www.cnblogs.com/arxobject/p/13882162.html