代码随想录算法训练营DAY11第五章 栈与队列part02

目录

[150. 逆波兰表达式求值](#150. 逆波兰表达式求值)

[239. 滑动窗口最大值](#239. 滑动窗口最大值)

[347. 前 K 个高频元素](#347. 前 K 个高频元素)


150. 逆波兰表达式求值

string转int方法:stk.push(stoi(t));

cpp 复制代码
class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> stk;
        for (string& t : tokens) {
            if(t=="+"||t=="-"||t=="*"||t=="/") {
                int b = stk.top();stk.pop();
                int a = stk.top();stk.pop();
                if (t == "+") stk.push(a + b);
                else if (t == "-")stk.push(a-b);
                else if(t=="*")stk.push(a*b);
                else stk.push(a/b);
            }
            else stk.push(stoi(t));
        }
        return stk.top();
    }
};

239. 滑动窗口最大值

优先队列

cpp 复制代码
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        priority_queue<pair<int,int>> q;
        vector<int> ans;
        for(int i=0;i<k;i++){
            q.emplace(nums[i],i);
        }
        ans.push_back(q.top().first);
        for(int i=k;i<nums.size();i++){
            q.emplace(nums[i],i);
            while(q.top().second<=i-k){
                q.pop();
            }
            ans.push_back(q.top().first);
        }
        return ans;
    }
};

单调队列(双端队列)

cpp 复制代码
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        deque<int> q;
        vector<int> ans;
        for (int i = 0; i < k; i++) {
            while (!q.empty() && nums[i] >= nums[q.back()]) {
                q.pop_back();
            }
            q.push_back(i);
        }
        ans.push_back(nums[q.front()]);
        for (int i = k; i < nums.size(); i++) {
            while (!q.empty() && nums[i] >= nums[q.back()]) {
                q.pop_back();
            }
            q.push_back(i);
            while (q.front() <= i - k) {
                q.pop_front();
            }
            ans.push_back(nums[q.front()]);
        }
        return ans;
    }
};

347. 前 K 个高频元素

哈希表

cpp 复制代码
class Solution {
    static bool com(pair<int, int>& a, pair<int, int>& b) {
        return a.second > b.second;
    }
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> map;
        for (int t : nums) {
            map[t]++;
        }
        vector<pair<int, int>> gra;
        for (auto& [key, value] : map) {
            gra.push_back({key, value});
        }
        sort(gra.begin(), gra.end(), com);
        vector<int> ans;
        for (int i = 0; i < k; i++) {
            ans.push_back(gra[i].first);
        }
        return ans;
    }
};

哈希表+桶排序

cpp 复制代码
class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> map;
        for(int t:nums){
            map[t]++;
        }
        vector<vector<int>> bucket(nums.size()+1);
        vector<int> ans;
        for(auto& [key,value]:map){
            bucket[value].push_back(key);
        }
        for(int i=nums.size();i>=1&&ans.size()<k;i--){
            for(int x:bucket[i]){
                ans.push_back(x);
            }
        }
        return ans;
    }
};
相关推荐
GuWenyue1 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法
我叫洋洋3 小时前
C ++ [ hello world ]
c语言·c++·算法
奋发向前wcx4 小时前
y1,y2总复习笔记5 2026.7.19
数据结构·笔记·算法
战族狼魂7 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一7 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH7 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
中达瑞和-高光谱·多光谱8 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机
香辣牛肉饭9 小时前
【算法】动态规划 最长公共子序列(LCS)
经验分享·笔记·算法·动态规划
旖-旎9 小时前
LeetCode 279:完全平方数(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题