Topic Tags: Backtracking
DFS use, to establish a tempList, was added to each recursive digital, using boolean [] used to skip numbers used, and skip duplicate combinations.
Specifically to see code.
Java Solution:
Runtime: 1 ms, faster than 99.60 %
Memory Usage: 41.7 MB, less than 11.94 %
Completion Date: 11/08/2019
The key points: the establishment of boolean [] used
class Solution { List<List<Integer>> res; public List<List<Integer>> permuteUnique(int[] nums) { res = new ArrayList<>(); List<Integer> tempList = new ArrayList<>(); boolean[] used = new boolean[nums.length]; Arrays.sort(nums); DFS(nums, tempList, used); return res; } private void DFS(int[] nums, List<Integer> tempList, boolean[] used) { if(tempList.size() == nums.length) { res.add(new ArrayList<>(tempList)); return; } for(int i=0 ; i<nums.length; i++) { if(used[i]) continue; if(i>0 && nums[i-1] == nums[i] && !used[i-1]) // if it is a duplicated permutation, skip continue; used[i] = true; tempList.add(nums[i]); DFS(nums, tempList, used); used[i] = false; tempList.remove(tempList.size() - 1); } } }
References: n / a
LeetCode title list - LeetCode Questions List
Topic Source: https: //leetcode.com/