Created
April 9, 2020 15:57
-
-
Save pot-code/e7e31dcc6a3a7b27eba819dc072f876b to your computer and use it in GitHub Desktop.
[data structure] heap and some stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MinHeap []int | |
func (h MinHeap) Len() int { return len(h) } | |
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] } | |
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } | |
func (h *MinHeap) Push(x interface{}) { | |
*h = append(*h, x.(int)) | |
} | |
func (h *MinHeap) Pop() interface{} { | |
old := *h | |
n := len(old) | |
x := old[n-1] | |
*h = old[0 : n-1] | |
return x | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment