My simple Java solution


  • 0
    V
        public int findPairs(int[] nums, int k) {
            int result = 0;
            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 1; i++) {
                if (i != 0 && nums[i] == nums[i - 1]) continue;  //  have the same
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[i] + k == nums[j]) {
                        result++;
                        break;
                    }
                }
            }
            return result;
        }
    }

Log in to reply
 

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