Java实现-带有重复元素的排列

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

样例

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

[
  [1,2,2],
  [2,1,2],
  [2,2,1]
]
class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    public List> permuteUnique(int[] nums) {
        // Write your code here
        Arrays.sort(nums);
		List> result=new ArrayList>();
		List tempList=new ArrayList();
		boolean used[]=new boolean[nums.length];
		backTracking(nums, result, tempList, used);
		return result;
    } 
    private static void backTracking(int[] nums, List> result, List tempList, boolean []used){
		if(nums.length==tempList.size()){
			result.add(new ArrayList(tempList));
		}else{
			for(int i=0;i0&&nums[i]==nums[i-1]&&used[i-1]==false||used[i]==true){
					continue;
				}else{
					used[i]=true;
					tempList.add(nums[i]);
					backTracking(nums, result, tempList, used);
					used[i]=false;
					tempList.remove(tempList.size()-1);
				}
			}
		}
	}

}


你可能感兴趣的:(斩杀LintCode,All,in,One,LintCode)