java using TwoPointer...and why when k<0 the result is 0


  • 0
    S
       public int findPairs(int[] nums, int k) {
        Arrays.sort(nums);
        if(k<0){
           return 0;
        }
        int res = 0;
        int low =0;
        int high = 1;
        while(low<nums.length && high < nums.length){
            int numsTmp = 0;
            if(nums[high] - nums[low] == k){
                res ++;
                 numsTmp = nums[low];
                while( low<nums.length && nums[low] == numsTmp ){
                    low++;
                }
                numsTmp = nums[high];
                while(high<nums.length && nums[high] == numsTmp ){
                    high++;
                }
            }else if(nums[high] - nums[low] < k){
                 numsTmp = nums[high];
               	 while( high<nums.length && nums[high] == numsTmp ){
                    high++;
                }
            }else if(nums[high] - nums[low] > k){
                numsTmp = nums[low];
                while( low<nums.length && nums[low] == numsTmp ){
                    low++;
                }
                
            }
             if(high == low){
                    high ++;
              }
        }
        return res;
    }

Log in to reply
 

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