mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
31 lines
538 B
Go
31 lines
538 B
Go
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
|
||
}
|