my Java solution


  • 0
    S

    public class Solution {
    public int findPairs(int[] nums, int k) {
    if (nums == null || nums.length == 0 || k < 0) return 0;

        int count=0;
        int l=0;
        int r=0;
        int size= nums.length;
        Arrays.sort(nums);
        while(r<size){
             while(r+1<size && nums[r] == nums[r+1]) {
                 r++; 
             }
            if(nums[r]-nums[l]==k && r!=l){
            	
                l++; r++;
                            
                count += 1;}
            else if(nums[r]-nums[l]>k)
            {
                l++;
            }
            else{
                r++;
            }
        }
        return count;
    }
    

    }


Log in to reply
 

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