[leetcode][hash] Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.size() < 2 || k <= 0) return false;
int start = 0, end = 0;
set<int> hashTable;
hashTable.insert(nums[0]);
for (int i = 1; i < nums.size(); ++i){
set<int>::iterator iter = hashTable.find(nums[i]);
if (iter != hashTable.end()) return true;
hashTable.insert(nums[i]);
end = i;
if (end - start == k){
hashTable.erase(nums[start]);
++start;
}
}
return false;
}
};

• 本文已收录于以下专栏：

LeetCode 217 Contains Duplicate（包含重复数字）（Vector、hash）

• NoMasp
• 2016-01-12 14:41
• 1430

LeetCode | Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices ...

【Leetcode Algorithm】Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices...

leetcode笔记--Contains Duplicate I & II & III

Contains Duplicate 题目：难度（Easy） Given an array of integers, find if the array contains any duplicat...

leetcode: (219) Contains Duplicate II

【Question】 Given an array of integers and an integer k, find out whether there are two distinct ind...
• zk031
• 2015-08-28 20:50
• 143

LeetCode 之 Contains Duplicate I II III — C++ 实现

Contains Duplicate   Given an array of integers, find if the array contains any duplicates. Your fu...

Leetcode 219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)