C++ short, no hashmap solution


  • 0
    M
        int findPairs(vector<int>& nums, int k) {
            if(nums.size()<2) return 0;
            int res=0, j=1;
            sort(nums.begin(), nums.end());
            for(int i=0;i<nums.size()-1;i++) {
                j=i+1;
                while(j<nums.size()) {
                    if(nums[j]>=nums[i]+k) {
                        if(nums[i]==nums[j]-k) res++;
                        break;
                    }
                    j++;
                }
                while(nums[i]==nums[i+1]) i++;
            }
            return res;
        }

Log in to reply
 

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