Contains Duplicate II

来源:互联网 发布:玩数码频道淘宝店铺 编辑:程序博客网 时间:2024/06/07 15:29

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 jis at most k.

class Solution {public:    bool containsNearbyDuplicate(vector<int>& nums, int k) {        map<int,int> temp;        for(int i=0;i<nums.size();i++){            if(temp.find(nums[i]) == temp.end()){                temp[nums[i]] = i;            }else if(i - temp[nums[i]]<=k){                return true;            }else{                temp[nums[i]] = i;            }        }        return false;    }};


0 0
原创粉丝点击