easy Set solution


  • 0
    Z
    class Solution {
        public int findPairs(int[] nums, int k) {
            if(nums.length==0) return 0;
            Arrays.sort(nums);
            Set<Integer> set = new HashSet<Integer>();
            int j=1;
            int i=0;
               while(i<nums.length&&j<nums.length){
                   if(Math.abs(nums[i]-nums[j])<k){
                       j++;
                   }else if(Math.abs(nums[i]-nums[j])>k){
                       i++;
                   }else{
                       if (i!=j) set.add(nums[i]+nums[j]);
                       j++;
                   }           
            }
            return set.size();
        }
    }
    

Log in to reply
 

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