Easy understand Python solution


  • 10
    def findPairs(self, nums, k):
            res = 0
            c = collections.Counter(nums)
            for i in c:
                if k > 0 and i + k in c or k == 0 and c[i] > 1:
                    res += 1
            return res
    

    which is equivalent to:

    def findPairs(self, nums, k):
            c = collections.Counter(nums)
            return  sum(k > 0 and i + k in c or k == 0 and c[i] > 1 for i in c)

  • 0

    brilliant idea


Log in to reply
 

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