前三大是哪几个
同一个堆。运行下面这段程序:
def sift_down(h, i, n):
while True:
big = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and h[l] > h[big]:
big = l
if r < n and h[r] > h[big]:
big = r
if big == i:
return
h[i], h[big] = h[big], h[i]
i = big
def pop(h):
top = h[0]
last = h.pop()
if len(h) > 0:
h[0] = last
sift_down(h, 0, len(h))
return top
h = [24, 23, 15, 13, 17]
out = []
for _ in range(3):
out.append(pop(h))
print("/".join(str(x) for x in out))
全部评论