代码随想录day11(补卡) 150. 逆波兰表达式求值 | 239. 滑动窗口最大值 | 347.前 K 个高频元素

150. 逆波兰表达式求值

题目链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/

思路:逆波兰式是后缀表达式,相当于后续遍历,用栈来实现,本题较容易。

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
       stack<long long> st;
       for (int i = 0; i< tokens.size(); i++){
        if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "/" || tokens[i] == "*"){
            long long num1 = st.top();
            st.pop();
            long long num2 = st.top();
            st.pop();
            if(tokens[i] == "+")
            st.push(num1 + num2);
            if(tokens[i] == "-")
            st.push(num2 - num1);
            if(tokens[i] == "*")
            st.push(num2 * num1);
            if(tokens[i] == "/")
            st.push(num2 / num1);
        }else{
            st.push(stoll(tokens[i]));
        }   
       }
       int result = st.top();
       st.pop();
       return result;  
    }
};

 239. 滑动窗口最大值

题目链接:https://leetcode.cn/problems/sliding-window-maximum/submissions/549976064/

思路:该题只能看卡哥的题解了,题目个人认为相当难了。要自己实现一个单调递减队列的类,还有不少判定条件。所以该题照着写了一遍以理解。

class Solution {
private:
    class Myqueue{   //单调队列
    public:
    deque<int> que;  //使用双端队列来实现单调队列
    void pop(int value){
        //每次弹出的时候比较要弹出的数值是否等于队列出口元素的数值,相等则弹出。同时pop之前判断队列是否为空。
        if(!que.empty() && value == que.front()){
            que.pop_front();
        }
    }
    void push(int value){
        while(!que.empty() && value > que.back()){
            que.pop_back();
        }
        que.push_back(value);
    }
       //查询当前队列里的最大值  直接返回队列前端front
       int front(){
        return que.front();
       } 
    };
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        Myqueue que;
        vector<int> result;
        for(int i = 0; i<k; i++){
            que.push(nums[i]);
        }
        result.push_back(que.front()); //result 记录前k个元素的最大值
        for (int i = k; i < nums.size();i++){
            que.pop(nums[i - k]); //滑动窗口移除最前面的元素
            que.push(nums[i]); //滑动窗口前加入最后面的元素
            result.push_back(que.front()); //记录对应的最大值
        }
        return result;
    }
};

347.前 K 个高频元素

题目链接:https://leetcode.cn/problems/top-k-frequent-elements/submissions/550851586/

思路:针对前K个高频元素,需要完成统计元素出现的频率、对频率排序、找出前k个高频元素这三步操作。利用小顶堆来统计最大前k个元素,每次将出现频率最小的元素弹出。题目挺难,需要多刷几次加深印象。

class Solution {
public:
//小顶堆
class mycomparison{
    public:
    bool operator()(const pair<int,int>& lhs, const pair<int,int>& rhs){
        return lhs.second > rhs.second;
    }
};
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> map;  //统计元素出现的频率 此处即为map<nums[i],对应出现的次数>
        for(int i = 0; i < nums.size(); i++){
            map[nums[i]]++;
        }
       // 对频率排序,定义一个小顶堆,大小为k
       priority_queue<pair<int,int>, vector<pair<int,int>>,mycomparison> priority_queue;
       for(unordered_map<int,int>::iterator it = map.begin(); it != map.end(); it++){
        priority_queue.push(*it);
        if (priority_queue.size() > k){
            //如果堆的大小大于k,则队列弹出
            priority_queue.pop();
        }
       }
       //找出前k个高频元素
       vector<int> result(k);
       for (int i=k - 1; i>=0;i--)
       {
        result[i] = priority_queue.top().first;
        priority_queue.pop();
       }
       return result;
    }
};

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值