Two pointer approach.


  • 0
    C
    public int findPairs(int[] nums, int k) {
        if(nums == null || nums.length == 0){
            return 0;
        }
        Arrays.sort(nums);
        int sameValue = nums[0];
        int count = 0;
        for(int i = 0;i < nums.length; i ++){
            if(i > 0 && nums[i] == sameValue){
                continue;
            } else {
                sameValue = nums[i];
            }
            for(int j = i + 1;j < nums.length; j++){
                if(nums[j] - nums[i] == k){
                    count ++;
                    break;
                }
            }
        }
        return count;  
    }

Log in to reply
 

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