LeetCode 229. 多数元素 II
题目描述
思路分析
思路描述
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func majorityElement(nums []int) []int {
counts := make(map[int]int)
var res []int
n := len(nums)
threshold := n / 3
// 统计每个数字出现的次数
for _, num := range nums {
counts[num]++
}
// 遍历统计结果,找到出现次数大于 n/3 的数字
for num, count := range counts {
if count > threshold {
res = append(res, num)
}
}
return res
}
1
write your code here
相似题目
题目 | 难度 | 题解 |
---|---|---|
多数元素 | Easy | |
检查一个数是否在数组中占绝大多数 | Easy |
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用