代码随想录算法训练营第11、12天 | 逆波兰表达式、滑动窗口最大值、前 K 个高频元素

逆波兰表达式题目
https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
逆波兰表达式代码随想录 https://programmercarl.com/0150.逆波兰表达式求值.html#其他语言版本
滑动窗口最大值
https://leetcode.cn/problems/sliding-window-maximum/
滑动窗口最大值代码随想录
https://programmercarl.com/0239.滑动窗口最大值.html
前K个高频元素
https://leetcode.cn/problems/top-k-frequent-elements/description/
前K个高频元素代码随想录
https://programmercarl.com/0347.前K个高频元素.html

逆波兰表达式

题目

给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。
请你计算该表达式。返回一个表示表达式值的整数。
注意:
有效的算符为 '+'、'-'、'*' 和 '/' 。

  • 每个操作数(运算对象)都可以是一个整数或者另一个表达式。
  • 两个整数之间的除法总是 向零截断 。
  • 表达式中不含除零运算。
  • 输入是一个根据逆波兰表示法表示的算术表达式。
    答案及所有中间计算结果可以用 32 位 整数表示。

题解

  • res栈 存放当前运算的两个数字
  • 在"/"和"-"的时候需要注意先后顺序
  • "/"的结果后 int()

题解代码

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        res = []
        item = ['*','/','+','-']
        for i in tokens:
            if i not in item:
                res.append(int(i))
            if i in item:
                node_1 = res.pop()
                node_2 = res.pop()
                if i == '*':
                    node = node_1*node_2
                    res.append(node)
                if i == "+":
                    node = node_1+node_2
                    res.append(node)
                if i=="-":
                    node = node_2-node_1
                    res.append(node)
                if i=="/":
                    node = int(node_2/node_1)
                    res.append(node)
        return res[0]

滑动窗口最大值

题目

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回 滑动窗口中的最大值 。

题解

  • 优先级队列 队列目前最前面的值为最大值;如果新的值大于该值,弹出再加入;
  • 模拟滑动窗口的步骤:1)先初始化滑动窗口 2)滑动窗口移动:1.先弹出 2.再加入;

题解代码

class myqueue:
    def __init__(self):
        self.queue = collections.deque()
    def push(self,value):
        num = len(self.queue)
        while self.queue and value>self.queue[-1]:
            self.queue.pop()
        self.queue.append(value)
    def pop(self,value):
        if self.queue and value==self.queue[0]:
            self.queue.popleft()
    def front(self):
        return self.queue[0]

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        queue = myqueue()
        for i in range(k):
            queue.push(nums[i])
        res = []
        res.append(queue.front())

        for i in range(k,len(nums)):
            queue.pop(nums[i-k])
            queue.push(nums[i])
            res.append(queue.front())
        return res

前 K 个高频元素

题目

给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。

题解

  • 重要数据结果:heapq python中的重点库
    操作:1)heapq.heappush(queue,(key,freq)) 2)heapq.heappop()
  • 先用map采集数据;然后用小堆栈排序

题解代码

import heapq
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        map_ = {}
        for i in range(len(nums)):
            map_[nums[i]] = map_.get(nums[i],0)+1
        ##收集map的dict
        ##用pri_que heapq.heappush和heapq.heappop
        pri_que = []
        for key,freq in map_.items():
            heapq.heappush(pri_que,(freq,key))
            if len(pri_que)>k:
                heapq.heappop(pri_que)
        res = []
        for i in range(len(pri_que)):
            res.append(heapq.heappop(pri_que)[1])
        return res[::-1]
posted @ 2024-06-14 22:25  哆啦**  阅读(2)  评论(0编辑  收藏  举报