I need help from someone.. I took me lot of time to debug this.


  • 0
    P

    This algorithm does work for all the test cases except one but I am not able to figure out what is causing this issue.

    The test case for which it fails is : [-1,2,0,-1,1,0,1]

    Can someone help me debug this issue:

    public class Solution {
    static List<List<Integer>> ans=null;
    public static List<List<Integer>> permuteUnique(int[] nums) {
    Arrays.sort(nums);
    ans = new ArrayList<List<Integer>>();
    permuteLoc(nums,0,nums.length);
    return ans;
    }

    	private static void permuteLoc(int[] nums, int start, int end) {
    		if(start==end){
    			//for(int i:nums)System.out.println(i);
    			List<Integer> perm = new ArrayList<Integer>();
    			for(int i=0;i<nums.length;i++){
    				perm.add(nums[i]);
    			//	System.out.print(nums[i]+" ");
    			}
    			//System.out.println();
    			ans.add(perm);
    		}
    		for(int k=start;k<end;k++){
    			if(start!=k && nums[start]==nums[k]){
    			   // k++;
    				continue;
    			}
    			int temp = nums[start];
    			nums[start]=nums[k];
    			nums[k]=temp;
    			permuteLoc(nums,start+1,end);
    			temp = nums[k];
    			nums[k]=nums[start];
    			nums[start]=temp;
    			/*
    			k++;
    			while(k<nums.length && nums[k]==nums[k-1])k++;
    			*/
    			while(k+1<nums.length && nums[k]==nums[k+1])k++;
    		}
    		
    	}
    
    }

Log in to reply
 

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