My code is correct in eclipse. But it is wrong when i put it to OJ


  • 1
    F

    The case is [-1,-1,3,-1] , The error is TLE

    public class Solution {
         ArrayList<ArrayList<Integer>> answerlist=new ArrayList<ArrayList<Integer>>();
         public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
             permute(num,0);
             return answerlist;
         }
    
        public void permute(int num[],int step)
        {
             if (step==num.length-1)
             {
                 ArrayList<Integer> answer=new ArrayList<Integer>();
                 for (int i=0;i<num.length;i++) answer.add(num[i]);
                 answerlist.add(answer);
             }
             else
             { 
                  permute(num,step+1);
                  for (int i=step+1;i<num.length;i++)
                  {
                       if (num[step]==num[i]) continue;
                       int temp;
                       temp=num[step];
                       num[step]=num[i];
                       num[i]=temp;
                
                       permute(num,step+1);
                
                       temp=num[i];
                       num[i]=num[step];
                       num[step]=temp;
                  }
            }
       }
    }

  • 0
    X

    I got the exactly same problem. Same idea and trapped by same test case.


  • 1
    S

    Think about this test case [2, 2, 1, 1]. You will generate duplicated lists. Maybe it is the reason of TLE.


  • 0
    S

    Thanks for your post. I just updated your post with correct code format, hope you will pay attention next time.


  • 0
    F

    thank you very much!


Log in to reply
 

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