Never give up！

###### [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 219: Contains Duplicate II

2015-05-29 15:02:09

#### 【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II（包含重复元素II）】

2015-08-29 06:36:21

#### leetcode_c++：Contains Duplicate II(219)

2016-06-18 13:33:40

#### 219. Contains Duplicate II [easy] (Python)

2016-06-14 19:59:14

#### leetcode笔记：Contains Duplicate II

2016-02-24 18:31:03

#### LeetCode 219. Contains Duplicate II（C++版）

2017-03-31 13:39:15

#### [C语言][LeetCode][219]Contains Duplicate II

2015-08-28 22:45:07

#### LeetCode 219:Contains Duplicate II

2015-05-31 18:41:22

#### leetcode 217: Contains Duplicate

2015-06-05 08:27:02

#### [leetcode-219]Contains Duplicate II（Ｃ）

2015-08-26 11:03:54

## 不良信息举报

[leetcode][hash] Contains Duplicate II