1 Star 0 Fork 0

xiangxiang/LeetCode-NOTES

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
solution.cpp 938 Bytes
Copy Edit Raw Blame History
默然 authored 7 years ago . update all algorithms.
class Solution
{
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
{
if(t < 0 | k <1)
return false;
int i,key;
unordered_map<int,int> dict;
for(i =0; i < nums.size(); i++)
{
key = nums[i]/max(1,t);
//map<int,int>::iterator it;
if( (dict.find(key) != dict.end() && abs(nums[i] - dict[key]) <= t) ||
(dict.find(key-1) != dict.end() && abs((long)nums[i] - (long)dict[key-1]) <= t) ||
(dict.find(key+1) !=dict.end() && abs(nums[i] - dict[key+1]) <= t))
{
return true;
}
dict.insert(pair<int,int>(key,nums[i]));
if(i >= k)
{
dict.erase(nums[i-k]/max(1,t)); //删除窗口大小之外的键值
}
}
return false;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiangxiang920/LeetCode-NOTES.git
git@gitee.com:xiangxiang920/LeetCode-NOTES.git
xiangxiang920
LeetCode-NOTES
LeetCode-NOTES
master

Search