share my solution


  • 0
    S
    public class Solution {
        public int findPairs(int[] nums, int k) {
            int count = 0;
            Arrays.sort(nums);
            int i = 0;
            while(i < nums.length) {
                for(int j = i+1; j < nums.length; j++) {
                    if(nums[j] == k + nums[i]) {
                        count++;
                        break;
                    } else if(nums[j] > k + nums[i]) {
                        break;
                   }
                }
                i++;
                while(i < nums.length && nums[i] == nums[i-1]) {
                    i++;
                }
            }
            return count;
        }
    }

Log in to reply
 

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