why this solution doesn't work


  • 0
    L

    public List<List<Integer>> permuteUnique(int[] nums) {
    List<List<Integer>> res = new ArrayList<>();
    if (nums == null || nums.length == 0) return res;
    Arrays.sort(nums);
    Set<Integer> indexes = new HashSet<Integer>();
    helper(res, new ArrayList<>(), indexes, nums);
    return res;
    }
    public void helper(List<List<Integer>> res, List<Integer> cur, Set<Integer> indexes, int[] nums){
    if (nums.length == cur.size()){
    res.add(new ArrayList<>(cur));
    return;
    }
    int i = 0;
    while (i < nums.length){
    if (!indexes.contains(i)){
    indexes.add(i);
    cur.add(nums[i]);
    helper(res, cur, indexes, nums);
    indexes.remove(new Integer(i));
    cur.remove(new Integer(nums[i]));
    int j = i + 1;
    while (j < nums.length && nums[i] == nums[j]) j++;
    i = j;
    }
    else {
    i = i + 1;
    }
    }
    return;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.