C++ solution with STL map


  • 0
    V

    class Solution

    {

    public:

    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
    
    {
        map<int,int> window;
        int windowStart = 0;
        int length = nums.size();
        for(int current = 0; current<length;current++)
        {
            if((current-windowStart) > k)
            {
                
                if(window[nums[windowStart]] == windowStart)
                    window.erase(nums[windowStart]);
                windowStart++;
            }
            map<int,int>::iterator iter = window.lower_bound(nums[current] -t);
            if(iter!=window.end() && abs(iter->first -nums[current]) <= t)
                return true;
            window[nums[current]] = current;
        }
        return false;
        
    }
    

    };


Log in to reply
 

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