2965. 找出缺失和重复的数字

找出缺失和重复的数字

解法一: 哈希

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func findMissingAndRepeatedValues(grid [][]int) []int {
n := len(grid)
countMap := make([]int, n*n+1)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
countMap[grid[i][j]]++
}
}
zero := 0
two := 0
for i := 1; i <= n*n; i++ {
if countMap[i] == 0 {
zero = i
} else if countMap[i] == 2 {
two = i
}
}
return []int{two, zero}
}
作者

wuhunyu

发布于

2024-05-31

更新于

2025-01-15

许可协议