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
| func topKFrequent(nums []int, k int) []int { m := map[int]int{} for _, v := range nums { m[v]++ } h := &IHeap{} heap.Init(h) for key, v := range m { heap.Push(h, [2]int{key, v}) if h.Len() > k { heap.Pop(h) } } res := make([]int, k) for i := 0; i < k; i++ { res[k-i-1] = heap.Pop(h).([2]int)[0] } return res }
type IHeap [][2]int
func (h IHeap) Len() int { return len(h) }
func (h IHeap) Less(i, j int) bool { return h[i][1] < h[j][1] }
func (h IHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IHeap) Push(x interface{}) { *h = append(*h, x.([2]int)) } func (h *IHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x }
|