Files
2020-08-06 23:29:19 +08:00

31 lines
538 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package structures
// intHeap 实现了 heap 的接口
type intHeap []int
func (h intHeap) Len() int {
return len(h)
}
func (h intHeap) Less(i, j int) bool {
return h[i] < h[j]
}
func (h intHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *intHeap) Push(x interface{}) {
// Push 使用 *h是因为
// Push 增加了 h 的长度
*h = append(*h, x.(int))
}
func (h *intHeap) Pop() interface{} {
// Pop 使用 *h ,是因为
// Pop 减短了 h 的长度
res := (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
return res
}