Force link :https://leetcode-cn.com/probl...
Their thinking :
- First, analyze the meaning of the topic by combining the topic stem , Given an array nums And a number k, Whether there is a subscript i,j So that two different subscripts do not differ by more than k I want to wait
- According to the meaning extracted above , You can turn the problem into , At a length of K+1(i-j<=k, Then count i In itself , The maximum length of the sliding window is k+1) To find out if there are two identical numbers , This is the time , The problem-solving model has been established
- There is an upper limit of the window , Then you need to discard the elements when you reach the upper limit , At this time, the leftmost element of the window is discarded , Subscript to be i-k-1(i-k For the first element in the sliding window , What needs to be discarded needs to be subtracted again )
- Last question , How to mark that an element has already appeared ? When solving array problems , We often use hash table to store tag bits
func containsNearbyDuplicate(nums []int, k int) bool {
numsToFlag := map[int]bool{}
for i := 0; i < len(nums); i++ {
if i > k {
numsToFlag[nums[i-k-1]] = false
}
if numsToFlag[nums[i]] {
return true
}
numsToFlag[nums[i]] = true
}
return false
}



![[high concurrency] deeply analyze the callable interface](/img/42/43d1f0b894f2632f2c7f1bfe970708.jpg)

![[structural mechanics] the reason why the influence line under joint load is different from that under direct load](/img/a6/fce0bb29cc5c84bc0ef20501617e06.png)


