Contains Duplicate II

来源:互联网 发布:vocaloid软件 编辑:程序博客网 时间:2024/05/17 01:28

Given an array of integers and an integer k, return true if and only if 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<int>& nums, int k) {        map<int, int> buf;    int n = nums.size();    for (int i = 0; i < n; i++)    {    if (buf.find(nums[i]) != buf.end() && i-buf[nums[i]] <= k)    {    return true;    }    else    {    buf[nums[i]] = i;    }    }    return false;    }};


0 0
原创粉丝点击