leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0532.cpp (529B)


      1 class Solution {
      2   public:
      3     int findPairs(vector<int> &nums, int k) {
      4         int res = 0;
      5         if (k == 0) {
      6             unordered_map<int, int> um;
      7             for (int n : nums)
      8                 um[n]++;
      9             for (const auto &[n, v] : um)
     10                 res += v >= 2;
     11             return res;
     12         } else {
     13             unordered_set<int> us(nums.begin(), nums.end());
     14             for (const auto &n : us)
     15                 res += us.count(n + k) + us.count(n - k);
     16             return res / 2;
     17         }
     18     }
     19 };