算法训练营第十一天|150. 逆波兰表达式求值、239. 滑动窗口最大值、347.前 K 个高频元素

150. 逆波兰表达式求值

题目

思路与解法

第一思路: 比较简单

python 复制代码
class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        for item in tokens:
            if item != '+' and item != '-' and item != '*' and item != '/' :
                stack.append(item)
            else:
                b = int(stack.pop())
                a = int(stack.pop())
                if item == '+':
                    stack.append(a + b)
                elif item == '-':
                    stack.append(a - b)
                elif item == '*':
                    stack.append(a * b)
                elif item == '/':
                    stack.append(a/b)
        return int(stack.pop())

239. 滑动窗口最大值

题目

思路与解法

第一思路:

carl的思路 : 滑动窗口解法,值得后面认真看看细节,归纳一下

python 复制代码
from collections import deque
class MyQueue(object): # 单调队列
    
    def __init__(self):
        self.queue = deque()

    def pop(self, value):
        if self.queue and value == self.queue[0]:
            return self.queue.popleft()

    def push(self, value):
        while self.queue and value > self.queue[-1]:
            self.queue.pop()
        self.queue.append(value)

    def front(self):
        return self.queue[0]


class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        myque = MyQueue()
        res = []
        lens = len(nums)
        i=0
        if i + k <= lens:
            while i < k:
                myque.push(nums[i])
                i += 1

        res.append(myque.front())
        i=1
        while i + k <= lens:
            myque.pop(nums[i-1])
            myque.push(nums[i+k-1])
            res.append(myque.front())
            i += 1
        return res

347.前 K 个高频元素

题目

思路与解法

第一思路: 先统计数量,在得出前k多的值。但是不知道怎么实现。有点被误导,因为要很技巧性,其实感觉很粗暴
carl的讲解: 先用字典统计,再将字典key-value互换,然后将互换后的keys(values)方法到list中,进行排序,得出按顺序的出现次数,再通过出现次数去找对应的值。

python 复制代码
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        from collections import defaultdict

        res = []

        item_dict = defaultdict(int)
        for item in nums:
            item_dict[item] += 1
        
        time_dict = defaultdict(list) 
        for key in item_dict.keys():
            time_dict[item_dict[key]].append(key)
        
        times = time_dict.keys()
        times = list(times)
        times.sort() # 从小往大
        count = 0 # 记载存入res中的总个数

        while count < k:
            res.extend(time_dict[times[-1]])
            count += len(time_dict[times[-1]])
            times.pop()
        
        return res
相关推荐
ArturiaZ2 分钟前
【day57】
开发语言·c++·算法
CoovallyAIHub4 分钟前
Energies | 8版YOLO对8版Transformer实测光伏缺陷检测,RF-DETR-Small综合胜出
深度学习·算法·计算机视觉
Emberone11 分钟前
排序:万物皆有序
算法·排序算法
其实秋天的枫13 分钟前
2025年12月英语六级真题及答案解析完整版(第一、二、三套全PDF)
经验分享·算法
2401_8747325319 分钟前
C++并发编程中的死锁避免
开发语言·c++·算法
2301_7923082521 分钟前
C++编译期数学计算
开发语言·c++·算法
hetao173383721 分钟前
2025-03-13~22 hetao1733837 的刷题记录
c++·算法
sqyno1sky33 分钟前
C++中的契约编程
开发语言·c++·算法
优化控制仿真模型1 小时前
2026年最新驾考科目一考试题库2309道全。电子版pdf
经验分享·算法·pdf
qq_334903151 小时前
嵌入式C++驱动开发
开发语言·c++·算法