K-diff Pairs in an Array
用哈希表就行了
class Solution(object):
def findPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
if k < 0:
return 0
store = collections.Counter(nums)
result = 0
for key, value in store.iteritems():
if k != 0 and key + k in store:
result += 1
if not k:
result = result + 1 if value >= 2 else result
return result