Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit c199122

Browse files
aQuaaQua
aQua
authored and
aQua
committed
kit 修改了 Priority Queue 的代码
1 parent c19c26b commit c199122

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

kit/PriorityQueue.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,17 @@ func (pq PQ) Swap(i, j int) {
3333

3434
// Push 往 pq 中放 entry
3535
func (pq *PQ) Push(x interface{}) {
36-
n := len(*pq)
37-
entry := x.(*entry)
38-
entry.index = n
39-
*pq = append(*pq, entry)
36+
temp := x.(*entry)
37+
temp.index = len(*pq)
38+
*pq = append(*pq, temp)
4039
}
4140

4241
// Pop 从 pq 中取出最优先的 entry
4342
func (pq *PQ) Pop() interface{} {
44-
old := *pq
45-
n := len(old)
46-
entry := old[n-1]
47-
entry.index = -1 // for safety
48-
*pq = old[0 : n-1]
49-
return entry
43+
temp := (*pq)[len(*pq)-1]
44+
temp.index = -1 // for safety
45+
*pq = (*pq)[0 : len(*pq)-1]
46+
return temp
5047
}
5148

5249
// update modifies the priority and value of an entry in the queue.

0 commit comments

Comments
 (0)