My two pointer clear Java solution Beats 92%


  • 0
    public int findPairs(int[] nums, int k) {
            int result = 0;
            Arrays.sort(nums);
            int slow = 0,fast = 1;
            while(fast < nums.length) {
                if(nums[slow] == nums[fast] - k) {
                    result++;
                    fast++;
                    slow++;
                    while(fast < nums.length && nums[fast] == nums[fast - 1]) fast++;
                }else if(nums[slow] < nums[fast] - k && slow < fast - 1) slow++;
                      else fast++;
            }
            return result;
        }

Log in to reply
 

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