-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathsingle_number.go
More file actions
62 lines (52 loc) · 833 Bytes
/
single_number.go
File metadata and controls
62 lines (52 loc) · 833 Bytes
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
56
57
58
59
60
61
62
package main
import (
"sort"
)
//只出现一次的数字
// 排序之后遍历查找
func singleNumber1(nums []int) int {
sort.Ints(nums)
for i := 0; i < len(nums)-1; i += 2 {
if nums[i] != nums[i+1] {
return nums[i]
}
}
return nums[len(nums)-1]
}
//使用一个哈希表
func singleNumber2(nums []int) int {
m := make(map[int]bool)
for _, n := range nums {
if m[n] {
delete(m, n)
} else {
m[n] = true
}
}
res := 0
for v := range m {
res = v
}
return res
}
//使用哈希表,记录元素出现的次数
func singleNumber3(nums []int) int {
m := make(map[int]int)
for _, n := range nums {
m[n] += 1
}
for k, v := range m {
if v == 1 {
return k
}
}
return -1
}
//位运算
func singleNumber4(nums []int) int {
res := 0
for _, n := range nums {
res ^= n
}
return res
}