算法训练营第十一天|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
相关推荐
一条大祥脚6 分钟前
26.1.26 扫描线+数论|因子反演+子序列计数|树套树优化最短路
数据结构·算法
m0_561359679 分钟前
基于C++的机器学习库开发
开发语言·c++·算法
星空露珠16 分钟前
速算24点所有题库公式
开发语言·数据库·算法·游戏·lua
2401_8324027517 分钟前
C++中的类型擦除技术
开发语言·c++·算法
努力学习的小廉25 分钟前
我爱学算法之—— 递归回溯综合(二)
开发语言·算法
sheji526126 分钟前
JSP基于信息安全的读书网站79f9s--程序+源码+数据库+调试部署+开发环境
java·开发语言·数据库·算法
2301_7634724627 分钟前
C++网络编程(Boost.Asio)
开发语言·c++·算法
依依yyy32 分钟前
沪深300指数收益率波动性分析与预测——基于ARMA-GARCH模型
人工智能·算法·机器学习
hcnaisd21 小时前
深入理解C++内存模型
开发语言·c++·算法
李老师讲编程1 小时前
C++信息学奥赛练习题-杨辉三角
数据结构·c++·算法·青少年编程·信息学奥赛