easy understand coding


  • -1
    H

    class Solution {
    public:
    int findPairs(vector<int>& nums, int k) {
    if(k<0) return 0;
    map<int,int>m;
    sort(nums.begin(),nums.end());
    for(int i=0;i<nums.size();i++){
    ++m[nums[i]];
    }
    map<int,int>::iterator it;
    it=m.begin();
    int res=0;
    if(k==0){
    while(it!=m.end()){
    if(it->second>=2) res++;
    ++it;
    }
    }else{
    while(it!=m.end()){
    if(m.count(it->first+k)!=0) res++;
    ++it;
    }
    }
    return res;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.