leetCode(28):Contains Duplicate II

来源:互联网 发布:java游戏大合集 编辑:程序博客网 时间:2024/05/17 03:44

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

哈希表的使用

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;    }};


0 0
原创粉丝点击