We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 62e64e9 commit 35ff9feCopy full SHA for 35ff9fe
Algorithms/0219.contains-duplicate-ii/contains-duplicate-ii.go
@@ -1,12 +1,14 @@
1
package Problem0219
2
3
func containsNearbyDuplicate(nums []int, k int) bool {
4
- for i, v := range nums {
5
- for j := i + 1; j <= i+k && j < len(nums); j++ {
6
- if v == nums[j] {
7
- return true
8
- }
+ // 利用 m 记录 a[i]中的值,每次出现的(索引号+1)
+ m := make(map[int]int, len(nums))
+
+ for i, n := range nums {
+ if m[n] != 0 && i-(m[n]-1) <= k {
9
+ return true
10
}
11
+ m[n] = i + 1
12
13
14
return false
0 commit comments