Skip to content

Commit

Permalink
[fix] [Python] g5c51o (labuladong#1401)
Browse files Browse the repository at this point in the history
* fix the use of heapq as priority queue

* fix methods
  • Loading branch information
onmoonno authored Jun 13, 2023
1 parent bbb1483 commit f3b9207
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -26417,8 +26417,9 @@ class Solution:
for v in nums:
valToFreq[v] = valToFreq.get(v, 0) + 1

# 优先队列按照键值对中的值(元素出现频率)从小到大排序
pq = [(freq, val) for val, freq in valToFreq.items()]
# 二叉堆按照键值对中的值(元素出现频率的负值)从小到大排列
# 从二叉堆中pop出来的就是频率最大的键 (频率越大,负值越小)
pq = [(-freq, val) for val, freq in valToFreq.items()]
heapq.heapify(pq)

# 将前 k 个最大元素装入 res
Expand Down Expand Up @@ -68764,39 +68765,43 @@ MyQueue.prototype.empty = function() {

```python
# by chatGPT (python)
class MyQueue:
class CQueue:
def __init__(self):
"""
初始化一个队列,使用两个堆栈 s1 和 s2
"""
self.s1 = []
self.s2 = []

def push(self, x: int) -> None:
def appendTail(self, value):
"""
添加元素到队尾
"""
self.s1.append(x)
self.s1.append(value)

def pop(self) -> int:
def deleteHead(self):
"""
删除队头的元素并返回
"""
# 先调用 peek 保证 s2 非空
self.peek()
return self.s2.pop()
if self.peek() is None:
return -1
else:
return self.s2.pop()

def peek(self) -> int:
def peek(self):
"""
返回队头元素
"""
if not self.s2:
# 把 s1 元素压入 s2
while self.s1:
self.s2.append(self.s1.pop())
return self.s2[-1]

return self.s2[-1] if self.s2 else None


def empty(self) -> bool:
def empty(self):
"""
判断队列是否为空
"""
Expand Down

0 comments on commit f3b9207

Please sign in to comment.