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].

思考:如何避免重複

想法

  • 1、將PermutationsI中的list換成map,利用map的key唯一性,避免重複
    1、將num中的元素當作map的key,將出現次數作爲value,設n爲 待插入數目(初始爲num.length),調用 recurPermutation(map, curNum, result, n);
    如果value大於0,則講key插入curNum列表,並將key的value - 1,
    3、將n置爲n - 1,遞歸,
    4、遞歸之後,將curNum和map復原(curNum移除key,map中key的value + 1),返回2步,繼續循環

  • 2、將問題視爲位置調換(leetcode 會超時,本地測試沒有問題)

    1、先將num排序,初始化i = 0,i爲換位的位置
    2、設k = i, k < n, k++ 以此將k與i位交換,遞歸i + 1位
    3、遞歸之後將k與i位換回,並進行下次循環
    

代碼

想法1

public class Solution {
    public List<List<Integer>> permuteUnique(int[] num) {
        List<List<Integer>> result = new LinkedList<List<Integer>>();     
        if(num.length == 0)
            return result;         
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> curNum = new LinkedList<Integer>();

        for(int i = 0; i < num.length; i++)
            if(map.containsKey(num[i]))
                map.put(num[i], map.get(num[i]) + 1);
            else
                map.put(num[i], 1);           
        recurPermutation(map, curNum, result, num.length);        
        return result; 
    }

    private void recurPermutation(Map<Integer, Integer> map, List<Integer> curNum, List<List<Integer>> result, int n){     
        if(n <= 0){
            result.add(new LinkedList<Integer>(curNum));
            return;
        }
        Set<Integer> keys = map.keySet();
        Iterator<Integer> it = keys.iterator();        
        while(it.hasNext()){
            Integer key = it.next();
            Integer value = map.get(key);
            if(value <= 0) continue;
            map.put(key, --value);
            curNum.add(key);
            recurPermutation(map, curNum, result, n - 1);
            curNum.remove(curNum.size() - 1);
            map.put(key, ++value);
        }
    }
}

想法2

public class Solution {
    public List<List<Integer>> permuteUnique(int[] num) {

        List<List<Integer>> result = new LinkedList<List<Integer>>();
        if(num.length == 0)
            return result;
        Arrays.sort(num);
        recurSwap(num, 0, num.length, result);
        return result;
    }

    private void recurSwap(int[] num, int index, int end, List<List<Integer>> result){

        if(index == end - 1){
            List<Integer> l = new LinkedList<>();
            for(int i:num)
                l.add(i);
            result.add(l);
            return;
        }
        for(int k = index; k < end; k++){

            if(k != index && num[k] == num[index]) continue;

            swap(index,k,num);
            recurSwap(num, index + 1, end, result);
            swap(index,k,num);
        }
    }
    private void swap(int i, int j, int[] num){
        int temp = num[j];
        num[j] = num[i];
        num[i] = temp;

    }
}
發佈了29 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章