Skip to content

Commit fed1698

Browse files
committed
添加 problem 532
1 parent 690af73 commit fed1698

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
func findPairs(nums []int, k int) int {
4+
if k < 0 || len(nums) == 0 {
5+
return 0
6+
}
7+
var count int
8+
m := make(map[int]int, len(nums))
9+
for _, value := range nums {
10+
m[value]++
11+
}
12+
for key, _ := range m {
13+
if k == 0 && m[key] > 1 {
14+
count++
15+
continue
16+
}
17+
if k > 0 && m[key+k] > 0 {
18+
count++
19+
}
20+
}
21+
return count
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question532 struct {
9+
para532
10+
ans532
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para532 struct {
16+
one []int
17+
n int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans532 struct {
23+
one int
24+
}
25+
26+
func Test_Problem532(t *testing.T) {
27+
28+
qs := []question532{
29+
30+
question532{
31+
para532{[]int{3, 1, 4, 1, 5}, 2},
32+
ans532{2},
33+
},
34+
35+
question532{
36+
para532{[]int{1, 2, 3, 4, 5}, 1},
37+
ans532{4},
38+
},
39+
40+
question532{
41+
para532{[]int{1, 3, 1, 5, 4}, 0},
42+
ans532{1},
43+
},
44+
45+
question532{
46+
para532{[]int{}, 3},
47+
ans532{0},
48+
},
49+
50+
// question532{
51+
// para532{[]int{1, 2, 3, 2, 3, 2, 3, 2}, 0},
52+
// ans532{[]int{1, 2, 3, 2, 3, 2, 3, 2}},
53+
// },
54+
55+
// question532{
56+
// para532{[]int{1, 2, 3, 4, 5}, 5},
57+
// ans532{[]int{1, 2, 3, 4}},
58+
// },
59+
60+
// question532{
61+
// para532{[]int{}, 5},
62+
// ans532{[]int{}},
63+
// },
64+
65+
// question532{
66+
// para532{[]int{1, 2, 3, 4, 5}, 10},
67+
// ans532{[]int{1, 2, 3, 4, 5}},
68+
// },
69+
70+
// question532{
71+
// para532{[]int{1}, 1},
72+
// ans532{[]int{}},
73+
// },
74+
}
75+
76+
fmt.Printf("------------------------Leetcode Problem 532------------------------\n")
77+
78+
for _, q := range qs {
79+
_, p := q.ans532, q.para532
80+
fmt.Printf("【input】:%v 【output】:%v\n", p, findPairs(p.one, p.n))
81+
}
82+
fmt.Printf("\n\n\n")
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [532. K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/)
2+
3+
## 题目
4+
5+
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
6+
7+
8+
Example 1:
9+
10+
```c
11+
Input: [3, 1, 4, 1, 5], k = 2
12+
Output: 2
13+
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
14+
Although we have two 1s in the input, we should only return the number of unique pairs.
15+
```
16+
17+
Example 2:
18+
19+
```c
20+
Input:[1, 2, 3, 4, 5], k = 1
21+
Output: 4
22+
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
23+
```
24+
25+
Example 3:
26+
27+
```c
28+
Input: [1, 3, 1, 5, 4], k = 0
29+
Output: 1
30+
Explanation: There is one 0-diff pair in the array, (1, 1).
31+
```
32+
33+
34+
Note:
35+
36+
1. The pairs (i, j) and (j, i) count as the same pair.
37+
2. The length of the array won't exceed 10,000.
38+
3. All the integers in the given input belong to the range: [-1e7, 1e7].
39+
40+
## 题目大意
41+
42+
43+
给定一个数组,在数组里面找到几组不同的 pair 对,每个 pair 对相差 K 。问能找出多少组这样的 pair 对。
44+
45+
46+
## 解题思路
47+
48+
这一题可以用 map 记录每个数字出现的次数。重复的数字也会因为唯一的 key,不用担心某个数字会判断多次。遍历一次 map,每个数字都加上 K 以后,判断字典里面是否存在,如果存在, count ++,如果 K = 0 的情况需要单独判断,如果字典中这个元素频次大于 1,count 也需要 ++。
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+

0 commit comments

Comments
 (0)