-
Notifications
You must be signed in to change notification settings - Fork 0
/
217_contains_duplicate.go
55 lines (47 loc) · 1.06 KB
/
217_contains_duplicate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package easy
import "sort"
/*Constraints:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
*/
func containsDuplicate(nums []int) bool {
return containsDuplicate1(nums)
}
/*
Runtime: 96 ms, faster than 52.85% of Go online submissions for Contains Duplicate.
Memory Usage: 7.8 MB, less than 96.81% of Go online submissions for Contains Duplicate.
*/
func containsDuplicate2(nums []int) bool { // less memory
numsLen := len(nums)
if numsLen == 1 {
return false
}
sort.Ints(nums)
for i := 1; i < numsLen; i++ {
if nums[i] == nums[i-1] {
return true
}
}
return false
}
/*
Runtime: 66 ms, faster than 85.17% of Go online submissions for Contains Duplicate.
Memory Usage: 9.3 MB, less than 30.31% of Go online submissions for Contains Duplicate.
*/
func containsDuplicate1(nums []int) bool { // more fast
numsLen := len(nums)
if numsLen == 1 {
return false
}
found := false
already := make(map[int]bool)
key := 0
for i := 0; i < numsLen; i++ {
key = nums[i]
if _, found = already[key]; found {
return true
}
already[key] = true
}
return false
}