/
githubmirror
/
LeetCode-Go
Обзор
Документация
Войти
/
githubmirror
/
LeetCode-Go
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
structures/Heap.go
30 строк
547 B
YDZ
Add solution 0119、1439
12 фев 2021, 07:43
12 фев 2021, 07:43
0881edf
Код
Авторство
О чём код?
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 }