您的位置:首页 > 编程语言 > C语言/C++

leetcode之219. Contains Duplicate II(C++解法)

2016-09-11 20:10 423 查看
题目:

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

[b]***************************[/b]我是分割线[b]*******************[/b]

class Solution {

public:

bool containsNearbyDuplicate(vector& nums, int k) {

unordered_map <int,int> coun;
for(int i=0; i<nums.size(); i++)
{
if (coun[nums[i]])
{
if( i + 1 - coun[nums[i]] <= k) return true;
else coun[nums[i]] = i + 1;
}
else coun[nums[i]] = i + 1;
}
return false;

}


};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++