package main
import (
"fmt"
"container/heap"
)
type heap_ []int
func (h heap_)Len() int {
return len(h)
}
func (h heap_)Less(i,j int) bool {
return h[i]<h[j]
}
func (h heap_)Swap(i,j int) {
h[i], h[j] = h[j], h[i]
}
func (h *heap_)Push(val interface{}) {
*h = append(*h, val.(int))
}
func (h *heap_)Pop() interface{} {
ret := (*h)[h.Len()-1]
*h = (*h)[:h.Len()-1]
return ret
}
func main() {
h := &heap_{}
heap.Init(h)
heap.Push(h,3)
heap.Push(h,7)
heap.Push(h,5)
heap.Push(h,2)
fmt.Println(h)
heap.Pop(h)
fmt.Println(h)
heap.Init(h)
fmt.Println(h)
}
container/heap包的使用
最新推荐文章于 2022-06-28 20:59:51 发布