代码随想录算法训练营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;
    }
};
相关推荐
BothSavage9 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn9 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽11 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法