Java 2 ms ( beat 99%)


  • 0
    A

    ...

    public boolean containsNearbyDuplicate(int[] nums, int k) {
             if(nums==null || nums.length==1)
               return false;
            int[] copy = nums.clone();        
        Arrays.sort(nums);
        int b= 0;
        int res =-1 ;        
        for(int i =0;i<nums.length-1;i++){
              if(nums[i]==nums[i+1]){
                     for(int x = 0;x<nums.length;x++) {
                          if(copy[x]== nums[i]){
                                   if(b==0){
                                        res =x;
                                        b=1;
                                    }
                                    else{
                                          res = x - res;
                                          if(res<=k)
                                             return true;
                                          res =x;
                                     }  
                            }
                       }
                      b=0;
                }              
          }
          return false;        
    }
    

    ...


Log in to reply
 

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