首页 > 学院 > 开发设计 > 正文

219. Contains Duplicate II

2019-11-11 07:28:21
字体:
来源:转载
供稿:网友

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; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表