219. Contains Duplicate II

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {

map<int, int>int_map;
map<int,int>::iterator iter;
for (int i = 0; i<nums.size(); i++) {
if((iter=int_map.find(nums[i]) )!= int_map.end() ){
if (i - iter->second <= k) {
return true;
}else{
int_map.erase(iter);
}
}
int_map.insert(pair<int, int>(nums[i], i));
}
return false;
}
};

posted @ 2016-04-27 21:03  maxandhchen  阅读(134)  评论(0编辑  收藏  举报