码迷,mamicode.com
首页 > 其他好文 > 详细

Contains Duplicate II

时间:2015-06-27 23:58:27      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

Description:

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

Code:

 1  bool containsNearbyDuplicate(vector<int>& nums, int k) {
 2         unordered_map<int,int>m;
 3         int n = nums.size();
 4         for (int i = 0; i < n; ++i)
 5         {
 6             if ( m.count(nums[i]) && i - m[nums[i]] <= k)
 7                 return true;
 8             else
 9                 m[nums[i]] = i;
10         }
11         return false;
12     }

 

Contains Duplicate II

标签:

原文地址:http://www.cnblogs.com/happygirl-zjj/p/4604850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!