Skip to content

Commit 598a46e

Browse files
committed
the key of set can be int
1 parent 61db5d1 commit 598a46e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Algorithms/K-diff Pairs in an Array/k-diff-pairs-in-an-array.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Source : https://leetcode.com/problems/k-diff-pairs-in-an-array/?tab=Description
22
# Author : Han Zichi
3-
# Date : 2017-03-06
3+
# Date : 2017-03-07
44

55
class Solution(object):
66
def findPairs(self, nums, k):
@@ -16,19 +16,18 @@ def findPairs(self, nums, k):
1616
dict = {}
1717

1818
for item in nums:
19-
key = str(item)
20-
if key in dict:
21-
dict[key] += 1
19+
if item in dict:
20+
dict[item] += 1
2221
else:
23-
dict[key] = 1
22+
dict[item] = 1
2423

2524
pre, ans = None, 0
2625
for item in nums:
2726
if item == pre:
2827
continue
29-
dict[str(item)] -= 1
28+
dict[item] -= 1
3029
target = item + k
31-
if str(target) in dict and dict[str(target)] > 0:
30+
if target in dict and dict[target] > 0:
3231
ans += 1
3332
pre = item
3433

0 commit comments

Comments
 (0)