码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Contains Duplicate II

时间:2015-05-30 16:27:22      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:

Contains Duplicate II 

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.

https://leetcode.com/problems/contains-duplicate-ii/

 

 


 

 

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} k
 4  * @return {boolean}
 5  */
 6 var containsNearbyDuplicate = function(nums, k) {
 7     var map = {};
 8     for(var i in nums){
 9         if(map[nums[i]] !== undefined){
10             if(Math.abs(map[nums[i]] - i) <= k){
11                 return true;
12             }
13         } else {
14             map[nums[i]] = i;
15         }
16     }
17     return false;
18 };

 

 

[LeetCode][JavaScript]Contains Duplicate II

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4540426.html

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