leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0532.cpp (529B)
0 class Solution {
1 public:
2 int findPairs(vector<int> &nums, int k) {
3 int res = 0;
4 if (k == 0) {
5 unordered_map<int, int> um;
6 for (int n : nums)
7 um[n]++;
8 for (const auto &[n, v] : um)
9 res += v >= 2;
10 return res;
11 } else {
12 unordered_set<int> us(nums.begin(), nums.end());
13 for (const auto &n : us)
14 res += us.count(n + k) + us.count(n - k);
15 return res / 2;
16 }
17 }
18 };