Java please


  • 0
    K

    public int findPairs(int[] nums, int k) {
    Arrays.sort(nums);
    int index=0;
    int i=0;
    while(i<nums.length-1){

        	 if(i!=0 && nums[i-1]==nums[i]){
    			 i++;
    		 }else{
    			 for(int j=i+1;j<nums.length;j++){
                     int res=Math.abs(nums[i]-nums[j]);
                     if(res==k){
                     	index++;
                     	i++;
                     	break;
                     }
                     else if(res > k){
                    	 i++;
                    	 break ;
                     }
                  
                    if(j==nums.length-1){
                    	i++;
                    	break;
                    }
                     
                 }
            	
            }
         
      }
        	
        	     	
     return index;
        
    }

Log in to reply
 

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