常见算法的进阶打怪路程|栈与队列 02

150. 逆波兰表达式求值 - 力扣(LeetCode)

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        # sm=0
        for i in tokens:
            if i in ['+', '-', '*', '/']:
                b = int(stack.pop())
                a = int(stack.pop())
                if i == '+':
                    stack.append(a + b)
                elif i == '-':
                    stack.append(a - b)
                elif i == '*':
                    stack.append(a * b)
                else:
                    stack.append(int(a / b))
            else:
                stack.append(int(i))
        print(stack)
        return stack.pop()

239. 滑动窗口最大值 - 力扣(LeetCode)

from collections import deque
class myque:
    def __init__(self):
        self.que=deque()
    def push(self,v):
        while self.que and self.que[-1]<v:
            self.que.pop()
        self.que.append(v)

    def pop(self,v):
        if self.que and self.que[0]==v:
            self.que.popleft()

    def get_max(self):
        return self.que[0]


class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        que=myque()
        re=[]
        for i in range(k):
            que.push(nums[i])
        re.append(que.get_max())
        for i in range(k,len(nums)):
            que.pop(nums[i-k])
            que.push(nums[i])
            re.append(que.get_max())
        return re

. - 力扣(LeetCode)

import heapq


class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        mapp = {}
        for i in nums:
            mapp[i] = mapp.get(i, 0) + 1

        hpq = []
        for i in mapp.keys():
            heapq.heappush(hpq, (mapp[i], i))
            if len(hpq) > k:
                heapq.heappop(hpq)

        re = [0] * k
        for i in range(k):
            re[k - i - 1] = heapq.heappop(hpq)[1]
        return re

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值