//一开始使用的map,后来发现用set就行了
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.size() == 0 || nums.size() == 1) return false;
set<int> dict;
int N = nums.size(), left = 0, right =min(k, N-1) , i=left;
for (int i = left; i <= right; i++) {
if (dict.find(nums[i]) == dict.end()) {//不存在
dict.insert(nums[i]);
}
else return true;
}
while (right < N-1) {
dict.erase(nums[left++]);
if (dict.find(nums[++right]) == dict.end()) {//不存在
dict.insert(nums[right]);
}
else return true;
}
return false;
}
};
int main()
{
Solution sol;
vector<int> input = { 1,2 };
int k = 2;
cout << sol.containsNearbyDuplicate(input, k);
return 0;
}