python中堆栈等数据结构

系统学习数据结构推荐《算法第四版》
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

heapq:(python中实现小根堆的模块)
"""Heap queue algorithm (a.k.a. priority queue).
堆算法(一种优先队列算法)
Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for
all k, counting elements from 0.  For the sake of comparison,
non-existing elements are considered to be infinite.  The interesting
property of a heap is that a[0] is always its smallest element.

依次输入元素:[3, 2, 1, 5, 6, 4]
每输入一个元素后输出建立的堆的结果(堆在形式上是一棵树,下面列表保存的是堆(或者树)的层次遍历顺序
[3]
[2, 3]
[1, 3, 2]
[1, 3, 2, 5]
[1, 3, 2, 5, 6]
[1, 3, 2, 5, 6, 4]

a[0] is always its smallest element。实现的是小根堆,数列保存的又是层次编列,所以a[0]永远最小
Usage:

heap = []            # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the smallest item from the heap
item = heap[0]       # smallest item on the heap without popping it
heapify(x)           # transforms list into a heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns smallest item, and adds
                               # new item; the heap size is unchanged

Our API differs from textbook heap algorithms as follows:

- We use 0-based indexing.  This makes the relationship between the
  index for a node and the indexes for its children slightly less
  obvious, but is more suitable since Python uses 0-based indexing.

- Our heappop() method returns the smallest item, not the largest.

These two make it possible to view the heap as a regular Python list
without surprises: heap[0] is the smallest item, and heap.sort()
maintains the heap invariant!
""
实现了的方法
__all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
           'nlargest', 'nsmallest', 'heappushpop']

给一个实列代码:

from typing import List
import heapq

# 找数组中第K个最大元素(python、堆实现) 


class Solution:

    # 使用容量为 k 的小顶堆
    # 元素个数小于 k 的时候,放进去就是了
    # 元素个数大于 k 的时候,小于等于堆顶元素,就扔掉,大于堆顶元素,就替换

    def findKthLargest(self, nums: List[int], k: int) -> int:
        size = len(nums)
        if k > size:
            raise Exception('程序出错')

        L = []
        for index in range(k):
            # heapq 默认就是小顶堆
            heapq.heappush(L, nums[index])
            print(L)

        for index in range(k, size):
            top = L[0]
            if nums[index] > top:
                # 看一看堆顶的元素,只要比堆顶元素大,就替换堆顶元素
                heapq.heapreplace(L, nums[index])
        # 最后堆顶中的元素就是堆中最小的,整个数组中的第 k 大元素
        return L[0]


test_l = [3, 2, 1, 5, 6, 4]
k = 5

a = Solution()
b = a.findKthLargest(test_l, k)
print(b)
代码来自leecode:也推荐大家用leetcode去练习一些算法题,知识点很丰富,题解也很全面大多数题提供多种语言实现,多做一些题既可以练习算法也可以熟悉语言(我就是这种想法)
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/kth-largest-element-in-an-array/solution/partitionfen-er-zhi-zhi-you-xian-dui-lie-java-dai-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值