Why is my code wrong?


  • 0
    Y
    	public   List<List<Integer>> permute(int[] nums) {
        if(nums.length==0)
        return null;
        
        List<List<Integer>> result=rank( nums,nums.length);
        return result;
    }
    
    public  void swap(int i,int j, int []nums){
        
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
    public  List<List<Integer>> rank(int[] nums,int length)
    {
       
       List<List<Integer>> result=new ArrayList<List<Integer>>();
        if(length<1)
         return null;
        for(int i=0;i<length;i++)
          {
             int cc=nums[i];
              swap(length-1,i,nums);
              for(List<Integer> temp:rank(nums,length--))
                {
                    temp.add(cc);
                    result.add(temp);
                }
               swap(length-1,i,nums); 
          }
        return result;
    }

  • 0
    J

    case when length is 1 needs to be handled


Log in to reply
 

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