219.有重复元素II(javascript)219.ContainsDuplicateII

原创
小哥 3年前 (2022-11-16) 阅读数 9 #大杂烩

题目来自: https://leetcode-cn.com/problems/contains-duplicate-ii/
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的 绝对值 至多为 k。

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.

示例 1:

输入: nums = [1,2,3,1], k = 3
输出: true

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true

示例 2:

输入: nums = [1,0,1,1], k = 1
输出: true

Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true

示例 3:

输入: nums = [1,2,3,1,2,3], k = 2
输出: false

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var containsNearbyDuplicate = function(nums, k) {
    //外循环i从0开始循环到倒数第二个数
    for(let i = 0;i
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除