Golang solution (49 ms) - sort and search


  • 0
    func findPairs(nums []int, k int) int {
        numPairs := 0
        sort.Ints(nums)
        for i := 0; i < len(nums) - 1; i++ {
            if i != 0 && nums[i] == nums[i - 1] {
                continue
            }
            for j := i + 1; j < len(nums) && nums[j] - nums[i] <= k; j++ {
                if nums[j] - nums[i] == k {
                    numPairs++
                    break
                }
            }
        }
        return numPairs
    }
    

Log in to reply
 

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