class Solution {
public:int getId(int a, int valuediff){// 值// return a/(valuediff+1);return a < 0 ? (a ) -) / (valuediff + 1) - 1 : a / (valuediff + 1);}public: unordered_map<int, int> bucket;bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {unordered_map<int, int> m;for(int i = 0; i < nums.size(); i++){int b = getId(nums[i], valueDiff);if(m.count(b))return true;else if(m.count(b-1) && abs(nums[i] - m[b-1])<=valueDiff){return true;}else if(m.count(b+1) && abs(nums[i] - m[b+1])<=valueDiff){return true;}m[b] = nums[i];if(i>=indexDiff)m.erase(getId(nums[i-indexDiff], valueDiff));}return false;}};
Thinking
1 位置 - 两层控制(只有这:m[b] = nums[i]; 控制位置。滑动窗口)
- 1)分桶
- 2) 分通后相邻部分的不确定用滑动窗口控制
2 值控制 if - else(都在控制值)