原题:
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.
Example 1:
Input: nums = [1,2,3,1], k = 3 Output: trueExample 2:
Input: nums = [1,0,1,1], k = 1 Output: trueExample 3:
Input: nums = [1,2,3,1,2,3], k = 2 Output: false
这个和上一个题相比麻烦就麻烦在要存储这个数上一次出现的位置,为此不得不看了看map的用法,结果:
Success
Runtime: 28 ms, faster than 93.62% of C++ online submissions for Contains Duplicate II.
Memory Usage: 15.4 MB, less than 24