From 811f15cf3c19d475db91c0fd3e57e4d85d997cfb Mon Sep 17 00:00:00 2001 From: Gaosong Date: Sun, 15 Jan 2023 11:50:04 +0800 Subject: [PATCH 1/2] fix: polling from an empty heap will cause panic --- codes/go/chapter_heap/my_heap.go | 1 + docs/chapter_heap/heap.md | 1 + 2 files changed, 2 insertions(+) diff --git a/codes/go/chapter_heap/my_heap.go b/codes/go/chapter_heap/my_heap.go index 0b3d0a3b1..cd3015f66 100644 --- a/codes/go/chapter_heap/my_heap.go +++ b/codes/go/chapter_heap/my_heap.go @@ -98,6 +98,7 @@ func (h *maxHeap) poll() any { // 判空处理 if h.isEmpty() { fmt.Println("error") + return nil } // 交换根结点与最右叶结点(即交换首元素与尾元素) h.swap(0, h.size()-1) diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 15d22e787..e1d29fe36 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -606,6 +606,7 @@ comments: true // 判空处理 if h.isEmpty() { fmt.Println("error") + return nil } // 交换根结点与最右叶结点(即交换首元素与尾元素) h.swap(0, h.size()-1) From ea3eee8b87f5beb36545ad32b1e6231a8c6d13af Mon Sep 17 00:00:00 2001 From: dshlstarr <99398488+dshlstarr@users.noreply.github.com> Date: Sun, 15 Jan 2023 17:17:52 +0800 Subject: [PATCH 2/2] Update quick_sort.md Fix the Golang code for median --- docs/chapter_sorting/quick_sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index 62ab2147f..01dbaec78 100644 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -483,7 +483,7 @@ comments: true func medianThree(nums []int, left, mid, right int) int { if (nums[left] > nums[mid]) != (nums[left] > nums[right]) { return left - } else if (nums[mid] < nums[left]) != (nums[mid] > nums[right]) { + } else if (nums[mid] > nums[left]) != (nums[mid] > nums[right]) { return mid } return right