Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 35ff9fe

Browse files
aQuaaQua
aQua
authored and
aQua
committed
219 improve
46ms
1 parent 62e64e9 commit 35ff9fe

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Algorithms/0219.contains-duplicate-ii/contains-duplicate-ii.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package Problem0219
22

33
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-
}
4+
// 利用 m 记录 a[i]中的值,每次出现的(索引号+1)
5+
m := make(map[int]int, len(nums))
6+
7+
for i, n := range nums {
8+
if m[n] != 0 && i-(m[n]-1) <= k {
9+
return true
910
}
11+
m[n] = i + 1
1012
}
1113

1214
return false

0 commit comments

Comments
 (0)