public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null){
return list;
}
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
helper(list, new ArrayList<>(), nums, used);
return list;
}
private void helper(List<List<Integer>> list, List<Integer> temp, int[] nums, boolean[] used){
if(temp.size() == nums.length){
list.add(new ArrayList<>(temp));
return;
}
for(int i = 0; i < nums.length; i++){
if(used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
continue;
}
used[i] = true;
temp.add(nums[i]);
helper(list, temp, nums, used);
used[i] = false;
temp.remove(temp.size() - 1);
}
}
}
[LeetCode] 47. Permutations II
最新推荐文章于 2021-02-02 11:47:10 发布