Skip to content

Commit 62e64e9

Browse files
aQuaaQua
aQua
authored and
aQua
committed
219 accepted
1126ms,最快的31ms
1 parent 6b81b75 commit 62e64e9

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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+
}
9+
}
10+
}
411

12+
return false
513
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0219
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,12 +15,12 @@ type question struct {
1515
// para 是参数
1616
type para struct {
1717
nums []int
18-
k int
18+
k int
1919
}
2020

2121
// ans 是答案
2222
type ans struct {
23-
one bool
23+
one bool
2424
}
2525

2626
func Test_Problem0219(t *testing.T) {
@@ -30,20 +30,30 @@ func Test_Problem0219(t *testing.T) {
3030

3131
question{
3232
para{
33-
,
33+
[]int{1, 2, 1, 3, 4, 5},
34+
2,
35+
},
36+
ans{
37+
true,
38+
},
39+
},
40+
41+
question{
42+
para{
43+
[]int{1, 2, 3, 4, 5},
44+
2,
3445
},
3546
ans{
36-
,
47+
false,
3748
},
3849
},
39-
4050
// 如需多个测试,可以复制上方元素。
4151
}
4252

4353
for _, q := range qs {
4454
a, p := q.ans, q.para
4555
fmt.Printf("~~%v~~\n", p)
4656

47-
ast.Equal(a.one, containsNearbyDuplicate(p.one), "输入:%v", p)
57+
ast.Equal(a.one, containsNearbyDuplicate(p.nums, p.k), "输入:%v", p)
4858
}
4959
}

0 commit comments

Comments
 (0)