[leetcode 220]Contains Duplicate III
2015-09-04 15:17
471 查看
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.
查找一个数组中是否存在两个元素,nums[i]和nums[j],其中两个元素的的差的绝对值|nums[i]-nums[j]|<=t,两个元素的下标|i-j|<=k.
建立下标与元素的映射关系,然后查找就好了。。。。
AC代码:
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.
查找一个数组中是否存在两个元素,nums[i]和nums[j],其中两个元素的的差的绝对值|nums[i]-nums[j]|<=t,两个元素的下标|i-j|<=k.
建立下标与元素的映射关系,然后查找就好了。。。。
AC代码:
class Solution { public: bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { if(k<=0||t<0) return false; int sum=nums.size(); if(sum<=1) return false; map<int,int> m; int j=0; for(int i=0;i<sum;++i) { if(i-j>k&&m[nums[j]]==j) m.erase(nums[j++]); map<int,int>::iterator ite=m.lower_bound(nums[i]-t); if(ite!=m.end()&&abs(ite->first-nums[i])<=t) return true; m[nums[i]]=i; } return false; } };其他leetcode题目AC代码:https://github.com/PoughER
相关文章推荐
- hdu-5237 Base64-The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
- 启用 csdn Again
- SSH整合报错:failed to lazily initialize a collection of role
- UVa 10976:Fractions Again?!(水题)
- 理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER
- http://blog.csdn.net/yes_life/article/details/6831856 根文件系统设备号
- AI (Adobe Illustrator)详细用法(三)
- 完全教程 Aircrack-ng破解WEP、WPA-PSK加密利器
- 北美18名校的数据挖掘,数据分析,人工智能及机器学习课程汇总
- RAID磁盘阵列特性简介
- FAILED Task attempt_xx_r_000000_0 failed to report status for 600 seconds
- [HDOJ1022]Train Problem I
- USACO 1.3 Barn Repair
- openwrt编译error: ext4_allocate_best_fit_partial: failed to allocate 13 blocks, out of space?
- DP - hdu5009 Paint Pearls
- EMAIL之SMTP应答序列
- whx带我刷JOI training
- Contains Duplicate II -- leetcode
- 新发现:AirDroid(用Web端控制自己的手机发信息)
- 音乐播放器-MainContentActivity简析