C++ 24ms (beat 92%)


  • 0
    S

    class Solution {
    public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {

        if (k==0) return false;
    
        unordered_set<int> win(k+1);
        long long int tt = t;
    
        for(int i=0;i<nums.size(); i++)
        {
    
            if (t>k)
            {
                unordered_set<int>::iterator itr = win.begin();
                for(;itr!=win.end();++itr)
                {
                    long long int res = nums[i]-*itr;
                    if (  abs(res) <= tt )
                    {
                        return true;
                    }
                }
            }
            else
            {
                int tmp = nums[i] - t;
                for(;tmp<=nums[i]+t;tmp++)
                {
                    if (win.find(tmp) != win.end())
                        return true;
                }               
            }
            win.insert(nums[i]);
            
            if (i>k-1)
                win.erase( nums[i-k]);
        }
        return false;
    }
    

    };


Log in to reply
 

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