Subject description:
Given an array of integers and k an integer, whether there are two different indices i and j is determined array, such nums [i] = nums [j], and the maximum absolute value of the difference is k i and j.
Example 1:
Input: the nums = [1,2,3,1], K =. 3
to true: Output
Example 2:
Input: the nums = [1,0,1,1], K =. 1
to true: Output
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Outline of Solution:
Note: title, said maximum value of k represents: the difference is less than the index k equal to
the value and the array index value and as a key into the dictionary,
if the current value occurred, the current index value is determined and dictionary index difference is less than equal to k, and if so, returns True
if not, or not occurred, the current value and its index into the dictionary.
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
dic={}
for i in range(len(nums)):
if nums[i] in dic:
if i-dic[nums[i]]<=k:
return True
dic[nums[i]]=i
return False