Given an array of integers and an integerk, find out whether there there are two distinct indicesin the array such thatnums[i] = nums[j]and the difference betweenis at mostk.
哈希表的使用
class Solution {public:bool containsNearbyDuplicate(vector<int>& nums, int k) {map<int,int> value;for(int i=0;i<nums.size();++i){if(value.find(nums[i])!=value.end() && i-value[nums[i]]<=k)return true;value[nums[i]]=i;}return false;}};
版权声明:本文为博主原创文章,,未经博主允许不得转载。
带上心灵去旅行,以平和的心态看待一切,