47. Permutations II

来源:互联网 发布:信越7783硅脂淘宝 编辑:程序博客网 时间:2024/05/18 03:32

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

//不懂 存在疑惑???public class Solution {    List<List<Integer>> result = new ArrayList<List<Integer>>(); //先定义    public List<List<Integer>> permuteUnique(int[] nums) {        Arrays.sort(nums); //进行排序        permutefunc(nums,nums.length,new ArrayList<Integer>(),new HashSet<Integer>()); //使用了HashSet        return result;    }     public void permutefunc(int[] nums,int k,List<Integer> array,Set<Integer> chosen){        if(k==0){            result.add(new ArrayList(array));            return;        }        for(int i=0;i<nums.length;i++){            if(i>0&&nums[i]==nums[i-1]&&!chosen.contains(i-1))continue;            if(chosen.add(i)){             array.add(nums[i]);             permutefunc(nums,k-1,array,chosen);             array.remove(array.size()-1);             chosen.remove(i);            }        }        return;    }}
0 0