-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path442-FindAllDuplicatesInAnArray.go
More file actions
66 lines (56 loc) · 1.68 KB
/
442-FindAllDuplicatesInAnArray.go
File metadata and controls
66 lines (56 loc) · 1.68 KB
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
63
64
65
66
package main
// 442. Find All Duplicates in an Array
// Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.
// You must write an algorithm that runs in O(n) time and uses only constant extra space.
// Example 1:
// Input: nums = [4,3,2,7,8,2,3,1]
// Output: [2,3]
// Example 2:
// Input: nums = [1,1,2]
// Output: [1]
// Example 3:
// Input: nums = [1]
// Output: []
// Constraints:
// n == nums.length
// 1 <= n <= 10^5
// 1 <= nums[i] <= n
// Each element in nums appears once or twice.
import "fmt"
// map
func findDuplicates(nums []int) []int {
m := make(map[int]int)
res :=[]int{}
for _,v := range nums {
m[v]++
if m[v] >= 2 {
res = append(res,v)
}
}
return res
}
func findDuplicates1(nums []int) []int {
var res []int
for _, num := range nums {
if num < 0 {
num *= -1
}
// n == nums.length
// 1 <= nums[i] <= n
if nums[num - 1] > 0 {
// 将下标的设置为负, 输入的为只为 1 <= n <= 10^5
nums[num - 1] *= -1
} else { // 如果小于 0 说明已在有一次存在了
res = append(res, num)
}
}
return res
}
func main() {
fmt.Println(findDuplicates([]int{4,3,2,7,8,2,3,1})) // [2,3]
fmt.Println(findDuplicates([]int{1,1,2})) // [1]
fmt.Println(findDuplicates([]int{1})) // []
fmt.Println(findDuplicates1([]int{4,3,2,7,8,2,3,1})) // [2,3]
fmt.Println(findDuplicates1([]int{1,1,2})) // [1]
fmt.Println(findDuplicates1([]int{1})) // []
}