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

題目:
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.
***************************我是分割線*******************
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;

}

};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章