Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4264f0a

Browse files
author
Sathish Babu
committed
✨ Added solution to 532
1 parent0e82dfa commit4264f0a

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-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(nums []int,kint)int {
4+
n:=len(nums)
5+
ifn<2||k<0 {
6+
return0
7+
}
8+
hash,c:=make(map[int]int),0
9+
for_,num:=rangenums {
10+
hash[num]++
11+
}
12+
ifk==0 {
13+
for_,v:=rangehash {
14+
ifv>=2 {
15+
c++
16+
}
17+
}
18+
}else {
19+
forkey:=rangehash {
20+
ifhash[key+k]>0 {
21+
c++
22+
}
23+
}
24+
}
25+
returnc
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
namestring
13-
inputsbool
14-
expectbool
13+
nums []int
14+
kint
15+
expectint
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
fori,c:=rangecases {
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,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)

[8]ページ先頭

©2009-2025 Movatter.jp