220. 存在重复元素 III

https://leetcode-cn.com/problems/contains-duplicate-iii/

https://leetcode-cn.com/problems/contains-duplicate-iii/solution/cjian-ji-dai-ma-by-orangeman-6/

class Solution {
public:
	bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
		set <long> s;
		for (int i = 0; i < nums.size(); ++i) {
			auto it = s.lower_bound(nums[i]-t);
			if (it != s.end() && *it <= (long)(nums[i] + t)) return true;
			if (i >= k + 1) s.erase(nums[i - k]);
		}
		return false;
	}
};

posted @ 2020-07-28 21:59  aaaaassss  阅读(64)  评论(0编辑  收藏  举报