-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path532.go
More file actions
59 lines (56 loc) · 900 Bytes
/
532.go
File metadata and controls
59 lines (56 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
func min(x int, y int) int {
if x < y {
return x
} else {
return y
}
}
func max(x int, y int) int {
if x > y {
return x
} else {
return y
}
}
func findPairs(nums []int, k int) int {
m := map[int]int{}
count := map[int]bool{}
res := 0
for _, val := range nums {
_, ok0 := m[val]
if ok0 {
m[val] += 1
} else {
m[val] = 1
}
if k == 0 {
_, okcount := count[val]
if m[val] > 1 && !okcount {
res++
count[val] = true
}
}
}
if k == 0 {
return res
}
for _, val := range nums {
//plus
_, ok1 := m[min(val, val-k)]
_, ok2 := m[max(val, val-k)]
_, ok3 := count[min(val, val-k)]
if ok1 && ok2 && !ok3 {
count[min(val, val-k)] = true
res++
}
//minus
_, ok1 = m[max(val, val+k)]
_, ok2 = m[min(val, val+k)]
_, ok3 = count[min(val, val+k)]
if ok1 && ok2 && !ok3 {
count[min(val, val+k)] = true
res++
}
}
return res
}