Leetcode 219 Contains Duplicate II
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (k == 0 || nums.size() == 1)
return false;
//使用c++11 新特性 unordered_map
unordered_map<int, int>map;
for (int i = 0; i < nums.size(); i++){
if (map.find(nums[i]) != map.end() && i - map[nums[i]] <= k)
return true;
else
map[nums[i]] = i;
}
return false;
}
};