您的位置:首页 > Web前端 > JavaScript

[LeetCode][JavaScript]Contains Duplicate II

2015-05-30 15:28 459 查看

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/

数组中两个相同值的数,下标之差要小于等于k。

/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var containsNearbyDuplicate = function(nums, k) {
var map = {};
for(var i in nums){
if(map[nums[i]]){
if(Math.abs(map[nums[i]] - i) <= k){
return true;
}
}
map[nums[i]] = i;
}
return false;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: