Java 12ms solution, no space


  • 0
     public int firstMissingPositive(int[] a) {
            for(int i=0;i<a.length;i++){
                if(!((a[i]<=0 || (a[i]>a.length))))
              {
               if(a[i]!=a[a[i]-1]){
                  int temp=a[a[i]-1];
                    a[a[i]-1]=a[i];
                    a[i]=temp;
                    i--;
                }
            }
            }
            int i=0;
            while(i<a.length && a[i]==i+1){
                i++;
            }
            return i+1;
        }
    
    

Log in to reply
 

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