LintCode 子集

题目

给出一个具有重复数字的列表,找出列表所有不同的排列。

样例
给出列表 [1,2,2],不同的排列有:

LintCode 子集_第1张图片
Paste_Image.png

代码

class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    public List> permuteUnique(int[] nums) {
        
        ArrayList> res = new ArrayList<>();
        
        if(nums == null)
            return null;
        
        if(nums.length == 0)
        {   
            res.add(new ArrayList());
            return res;
        }
        
        ArrayList list = new ArrayList<>();
        
        //先将数组排序,这样相同元素将会出现在一起
        Arrays.sort(nums);
        
        int n = nums.length;
        int[] visited = new int[n];
        for(int i=0;i> res, ArrayList list, int[] visited, int[] nums) {
        
        if(nums.length == list.size()) {
            res.add( new ArrayList(list));
        }
        
        for(int i=0;i

你可能感兴趣的:(LintCode 子集)