算法训练营第十一天|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 分钟前
【回文 字符串】3677 统计二进制回文数字的数目|2223
c++·算法·字符串·力扣·回文
Tisfy13 分钟前
LeetCode 0085.最大矩形:单调栈
算法·leetcode·题解·单调栈
mit6.82414 分钟前
出入度|bfs|状压dp
算法
hweiyu0015 分钟前
强连通分量算法:Kosaraju算法
算法·深度优先
源代码•宸15 分钟前
Golang语法进阶(定时器)
开发语言·经验分享·后端·算法·golang·timer·ticker
mit6.82422 分钟前
逆向思维|memo
算法
机器学习之心23 分钟前
MATLAB灰狼优化算法(GWO)改进物理信息神经网络(PINN)光伏功率预测
神经网络·算法·matlab·物理信息神经网络
代码游侠27 分钟前
学习笔记——ESP8266 WiFi模块
服务器·c语言·开发语言·数据结构·算法
倦王28 分钟前
力扣日刷26110
算法·leetcode·职场和发展
涛涛北京39 分钟前
【算法比较】
算法