Different results when running 21st test in local and leetcode.


  • 0
    H

    Below is my code.

    public List<List<Integer>> permuteUnique(int[] nums) {
    	if (nums == null || nums.length == 0) {
    		return new ArrayList<>();
    	}
    	Arrays.sort(nums);
    	List<List<Integer>> list = new ArrayList<>();
    	helper(nums, list, 0);
    	return list;
    }
    
    private void helper(int[] nums, List<List<Integer>> list, int cur) {
    	if (cur == nums.length) {
    		List<Integer> temp = new ArrayList<>();
    		for (int num : nums) {
    			temp.add(num);
    		}
    		list.add(temp);
    	}
    	for (int i = cur; i < nums.length; i++) {
    		if (i > cur) {
    			while (i < nums.length && nums[i] == nums[i - 1]) {
    				i++;
    			}
    			if (i == nums.length) {
    				break;
    			}
    		}
    		swap(nums, i, cur);
    		helper(nums, list, cur + 1);
    		swap(nums, i, cur);
    	}
    }
    
    private void swap(int[] nums, int i, int j) {
    	int temp = nums[i];
    	nums[i] = nums[j];
    	nums[j] = temp;
    }
    

    }

    Result in leetcode when running 21st test.

    My code fails the 21st test but I don't know where is the problem?
    0_1505280316963_7401235a-f627-429c-a98d-d68f31672e4d-image.png
    Result in local when running 21st test.
    0_1505280369925_0059a89b-a8e2-49fa-9c90-4bb163d52a78-image.png

    obviously, the size of result set in local is more than that in leetcode. So where is the problem? Is there any mistake in my code.


Log in to reply
 

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