网络建设网站有关知识,高端轻奢品牌,网站建设分为哪几部分,南安市建设局网站文章目录1. 题目2. 解题1. 题目
给定一个整数数组#xff0c;判断数组中是否有两个不同的索引 i 和 j#xff0c;使得 nums [i] 和 nums [j] 的差的绝对值最大为 t#xff0c;并且 i 和 j 之间的差的绝对值最大为 ķ。
示例 1:输入: nums [1,2,3,1], k 3, t 0
输出: tr…
文章目录1. 题目2. 解题1. 题目
给定一个整数数组判断数组中是否有两个不同的索引 i 和 j使得 nums [i] 和 nums [j] 的差的绝对值最大为 t并且 i 和 j 之间的差的绝对值最大为 ķ。
示例 1:输入: nums [1,2,3,1], k 3, t 0
输出: true
示例 2:输入: nums [1,0,1,1], k 1, t 2
输出: true
示例 3:输入: nums [1,5,9,1,5,9], k 2, t 3
输出: false来源力扣LeetCode 链接https://leetcode-cn.com/problems/contains-duplicate-iii 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 解题
建立set判断 nums[i] - t为下限的set的迭代器it 迭代器不等于 end那找到了就是 set中的最小的数*it
class Solution {
public:bool containsNearbyAlmostDuplicate(vectorint nums, int k, int t) {setlong s;for(int i 0; i nums.size(); i){auto it s.lower_bound(long(nums[i])-long(t));if(it ! s.end() *it long(nums[i])long(t))return true;else{s.insert(nums[i]);if(s.size() k)s.erase(nums[i-k]);}}return false;}
};