2670. 找出不同元素数目差数组

2670. 找出不同元素数目差数组

解法一: 哈希

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func distinctDifferenceArray(nums []int) []int {
leftCount := make(map[int]int)
rightCount := make(map[int]int)
for _, num := range nums {
rightCount[num]++
}
n := len(nums)
ans := make([]int, n)
for i, num := range nums {
rightCount[num]--
if rightCount[num] == 0 {
delete(rightCount, num)
}
leftCount[num]++
ans[i] = len(leftCount) - len(rightCount)
}
return ans
}
作者

wuhunyu

发布于

2024-01-31

更新于

2024-01-31

许可协议