-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0532-KDiffPairsInAnArray.cs
39 lines (34 loc) · 1.06 KB
/
0532-KDiffPairsInAnArray.cs
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
//-----------------------------------------------------------------------------
// Runtime: 148ms
// Memory Usage: 30.6 MB
// Link: https://leetcode.com/submissions/detail/359589168/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0532_KDiffPairsInAnArray
{
public int FindPairs(int[] nums, int k)
{
if (k < 0) return 0;
var counts = new SortedDictionary<int, int>();
foreach (var num in nums)
{
if (!counts.ContainsKey(num))
counts[num] = 1;
else
counts[num]++;
}
var count = 0;
if (k == 0)
foreach (var key in counts.Keys)
{
if (counts[key] > 1) count++;
}
else
foreach (var key in counts.Keys)
if (counts.ContainsKey(key + k)) count++;
return count;
}
}
}