首页 > 编程语言 > 详细

[LeetCode][JavaScript]Contains Duplicate II

时间:2015-05-30 16:25:21      阅读:347      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!