219. Contains Duplicate II

来源:互联网 发布:昂飞基因芯片数据分析 编辑:程序博客网 时间:2024/06/15 23:39

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 absolute difference between i and j is at most k.

class Solution {public:    bool containsNearbyDuplicate(vector<int>& nums, int k) {        int size = nums.size();        map<int, int> m;        for(int i = 0; i < size; ++i){            if(m.count(nums[i])){                if(i - m[nums[i]] <= k)                    return true;            }            m[nums[i]] = i;        }        return false;    }};
0 0
原创粉丝点击