LeetCode 47. Permutations II
原题链接在这里:https://leetcode.com/problems/permutations-ii/
题目:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
题解:
nums中有重复元素,为了避免加重复值,需要去重.
去重的方法有两种,一个是加结果之前用contains看是否有重复结果,另外一个是找结果之前比较它是否与前一个元素相同。
Recursion方法和Permutations相同,但为了去重,不但需要比较与之前元素是否相同,还需要看之前元素是否被use过, 如果usd[i-1] = true 说明前个相同的值已经在item里, 那么没有关系.
但若之前元素没有被用过那么就需要跳过.
Time Complexity: exponenetial.
Space: O(nums.length). stack space. regardless res.
AC Java:
1 class Solution { 2 public List<List<Integer>> permuteUnique(int[] nums) { 3 List<List<Integer>> res = new ArrayList<List<Integer>>(); 4 boolean [] used = new boolean[nums.length]; 5 Arrays.sort(nums); 6 dfs(nums, used, new ArrayList<Integer>(), res); 7 return res; 8 } 9 10 private void dfs(int [] nums, boolean [] used, List<Integer> item, List<List<Integer>> res){ 11 if(item.size() == nums.length){ 12 res.add(new ArrayList<Integer>(item)); 13 return; 14 } 15 16 for(int i = 0; i<nums.length; i++){ 17 if(i>0 && !used[i-1] && nums[i]==nums[i-1]){ 18 continue; 19 } 20 if(!used[i]){ 21 used[i] = true; 22 item.add(nums[i]); 23 dfs(nums, used, item, res); 24 item.remove(item.size()-1); 25 used[i] = false; 26 } 27 28 } 29 } 30 }
AC Python:
1 class Solution(object): 2 def permuteUnique(self, nums): 3 res = [] 4 nums.sort() 5 self.dfs(nums, [False] * len(nums), [], res) 6 return res 7 8 def dfs(self, nums, used, item, res): 9 if len(item) == len(nums): 10 res.append(item[:]) 11 return 12 13 for i in range(len(nums)): 14 if i > 0 and not used[i - 1] and nums[i] == nums[i - 1]: 15 continue 16 17 if not used[i]: 18 used[i] = True 19 item.append(nums[i]) 20 self.dfs(nums, used, item, res) 21 item.pop() 22 used[i] = False 23
Iteration方法和Subsets II很像,加进newRes之前检查newRes是否包含了重复item, 没有重复才可以加进来.
Time Complexity: exponential. Space: O(res.size()).
AC Java:
1 public class Solution { 2 public List<List<Integer>> permuteUnique(int[] nums) { 3 List<List<Integer>> res = new ArrayList<List<Integer>>(); 4 if(nums == null || nums.length == 0){ 5 return res; 6 } 7 List<Integer> item = new ArrayList<Integer>(); 8 item.add(nums[0]); 9 res.add(item); 10 for(int i = 1; i<nums.length; i++){ 11 List<List<Integer>> newRes = new ArrayList<List<Integer>>(); 12 for(int j = 0; j<res.size(); j++){ 13 List<Integer> cur = res.get(j); 14 for(int k=0;k<cur.size()+1;k++){ 15 item = new ArrayList<Integer>(cur); 16 item.add(k,nums[i]); 17 if(!newRes.contains(item)){ 18 newRes.add(item); 19 } 20 } 21 } 22 res = newRes; 23 } 24 return res; 25 } 26 }
类似Permutations.