算法训练营第十一天|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
相关推荐
好易学·数据结构3 小时前
可视化图解算法57:字符串的排列
数据结构·算法·leetcode·面试·笔试·回溯算法·牛客
এ᭄画画的北北5 小时前
力扣-283.移动零
算法·leetcode
2501_924879368 小时前
口罩识别场景误报率↓79%:陌讯多模态融合算法实战解析
人工智能·深度学习·算法·目标检测·智慧城市
Christo38 小时前
TFS-2022《A Novel Data-Driven Approach to Autonomous Fuzzy Clustering》
人工智能·算法·机器学习·支持向量机·tfs
木木子99998 小时前
超平面(Hyperplane)是什么?
算法·机器学习·支持向量机·超平面·hyperplane
星空下的曙光10 小时前
React 虚拟 DOM Diff 算法详解,Vue、Snabbdom 与 React 算法对比
vue.js·算法·react.js
♞沉寂10 小时前
数据结构——双向链表
数据结构·算法·链表
大阳12310 小时前
数据结构2.(双向链表,循环链表及内核链表)
c语言·开发语言·数据结构·学习·算法·链表·嵌入式
CUC-MenG11 小时前
2025牛客多校第六场 D.漂亮矩阵 K.最大gcd C.栈 L.最小括号串 个人题解
c语言·c++·算法·矩阵
2401_8762213411 小时前
Tasks and Deadlines(Sorting and Searching)
c++·算法