leetcode 220 Contains Duplicate III
2015-09-14 19:43
495 查看
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) { if (!k || t<0 || nums.size()<2) return false; //使用了set可以排序,大小为k,滑动窗口浏览,看窗口内部有没有小于t的 //重复:就必然存在,不重复:就可以用set set<int> record; auto nLen = nums.size(); for (int i = 0; i < nLen;++i) { if (i>k) record.erase(nums[i - k - 1]); //两个相同时,早已return true set<int>::iterator lower = record.lower_bound(nums[i] - t); if (lower != record.end() && abs(nums[i] - *lower) <= t) return true; record.insert(nums[i]); } return false; } };
相关文章推荐
- linux 中/proc 详解 http://blog.csdn.net/kevinx_xu/article/details/8178746
- main函数可否被递归调用
- poj1363Rails(栈模拟)
- jedis高版本,JedisPoolConfig,maxActive属性,maxWait,配置maxActive,maxTotal,maxWaitMillis
- AIX常用
- POJ1681 Painter's Problem【高斯消元法】
- Climbing Stairs
- 配置sendmail转发邮件
- jerbrains产品算号
- OnInitUpdate、OnUpdate、OnDraw与OnPaint!
- 如何解决DrawerLayout打开时, main content view 中的按钮仍然可以被点击的问题?
- linux设备模型详解 http://blog.csdn.net/linux_xiaomugua/article/details/6989386
- [Leetcode]Paint House
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- LeetCode_Submission Details
- aircrack加reaver破解带有wps的wifi
- Failed to fetch URL https://dl-ssl.google.com/glass/gdk/addon.xml, reason: HttpHostConnect Connectio
- AI资源
- RAID技术
- HDU 5009 Paint Pearls