8000 :sparkles: Added solution to 532 · reverse/awesome-golang-leetcode@4264f0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4264f0a

Browse files
author
Sathish Babu
committed
✨ Added solution to 532
1 parent 0e82dfa commit 4264f0a

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

src/0532.K-diff-Pairs-in-an-Array/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,31 @@
55
66
## Description
77

8+
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.
9+
810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: [3, 1, 4, 1, 5], k = 2
14+
Output: 2
15+
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
16+
Although we have two 1s in the input, we should only return the number of unique pairs.
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input:[1, 2, 3, 4, 5], k = 1
23+
Output: 4
24+
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: [1, 3, 1, 5, 4], k = 0
31+
Output: 1
32+
Explanation: There is one 0-diff pair in the array, (1, 1).
1333
```
1434

1535
## 题意
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
n := len(nums)
5+
if n < 2 || k < 0 {
6+
return 0
7+
}
8+
hash, c := make(map[int]int), 0
9+
for _, num := range nums {
10+
hash[num]++
11+
}
12+
if k == 0 {
13+
for _, v := range hash {
14+
if v >= 2 {
15+
c++
16+
}
17+
}
18+
} else {
19+
for key := range hash {
20+
if hash[key+k] > 0 {
21+
c++
22+
}
23+
}
24+
}
25+
return c
526
}

src/0532.K-diff-Pairs-in-an-Array/Solution_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{3, 1, 4, 1, 5}, 2, 2},
18+
{"TestCase", []int{1, 2, 3, 4, 5}, 1, 4},
19+
{"TestCase", []int{1, 3, 1, 5, 4}, 0, 1},
20+
{"TestCase", []int{1, 3, 1, 5, 4}, -1, 0},
21+
{"TestCase", []int{1}, 0, 0},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
27+
got := Solution(c.nums, c.k)
2528
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, 52E2 got, c.inputs)
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
30+
c.expect, got, c.nums, c.k)
2831
}
2932
})
3033
}

0 commit comments

Comments
 (0)
0