Leetcode175: Contains Duplicate III

来源:互联网 发布:淘宝客服问题总结 编辑:程序博客网 时间:2024/06/08 12:01

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.

class Solution {public:    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {        map<long long, int> m;        int j = 0;        for (int i = 0; i < nums.size(); ++i) {            if (i - j > k && m[nums[j]] == j) m.erase(nums[j++]);            auto a = m.lower_bound(nums[i] - t);            if (a != m.end() && abs(a->first - nums[i]) <= t) return true;            m[nums[i]] = i;        }        return false;    }};


0 0
原创粉丝点击