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 difference between i and j is at most k.
题意:
给定整数数组和整数k,找出数组中是否有两个不同的索引i和j,使得nums [i] = nums [j],i和j之间的差最多为k。
思路:
利用hash表,key为元素值,value为此元素所对应的索引的集合,遍历元素组,把相同元素的索引都放入到对应hash表的value集合中,然后遍历hash表,找出集合大小大于1的value集合,遍历此集合,求出相同元素索引间最小距离。
1 | class Solution { |
方法二:
思路同方法一,利用hash表,不同点是一边遍历数组,一边在hash表对应的索引值进行相减比较。
1 | class Solution { |