47. Permutations II

public class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> ret=new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        boolean[] used=new boolean[nums.length];
        permute(new ArrayList<Integer>(), used, ret, nums);
        return ret;
    }
    private void permute(List<Integer> list, boolean[] used, List<List<Integer>> ret, int[] nums){
        if(list.size()==nums.length)
            ret.add(new ArrayList<Integer>(list));
        else
            for(int i=0;i<nums.length;i++)
                if(used[i]==false)
                {
                    if(i>0&&nums[i]==nums[i-1]&&used[i-1]==false)
                        continue;
                    used[i]=true;
                    list.add(nums[i]);
                    permute(list, used, ret, nums);
                    list.remove(list.size()-1);
                    used[i]=false;
                }
    }
}

 

posted @ 2017-09-25 08:07  Weiyu Wang  阅读(122)  评论(0编辑  收藏  举报