Files
LeetCode-Go/leetcode/0219.Contains-Duplicate-II/219. Contains Duplicate II.go
2020-08-07 17:06:53 +08:00

22 lines
362 B
Go

package leetcode
func containsNearbyDuplicate(nums []int, k int) bool {
if len(nums) <= 1 {
return false
}
if k <= 0 {
return false
}
record := make(map[int]bool, len(nums))
for i, n := range nums {
if _, found := record[n]; found {
return true
}
record[n] = true
if len(record) == k+1 {
delete(record, nums[i-k])
}
}
return false
}