找工作准备刷题day1 栈与队列

第一题:LeetCode150.逆波兰表达式求值

题解

class Solution {
public:
    bool isNumber(const string& str) {
        return !(str == "+" || str == "-" || str == "*" || str == "/");
    }

    int evalRPN(vector<string>& tokens) {
        stack<int> mystack;
        for (auto token : tokens) {
            if (isNumber(token))
                mystack.push((int)atoi(token.c_str()));
            else {
                int n2 = mystack.top();
                mystack.pop();
                int n1 = mystack.top();
                mystack.pop();
                switch (token[0]) {
                case '+':
                    mystack.push(n1 + n2);
                    break;
                case '-':
                    mystack.push(n1 - n2);
                    break;
                case '*':
                    mystack.push(n1 * n2);
                    break;
                case '/':
                    mystack.push(n1 / n2);
                    break;
                }
            }
        }

        return mystack.top();
    }
};

知识点

  1. 注意n1 n2 的顺序,除法顺序很重要;
  2. atoi 将一个字符串转换成int,string.c_str() 将string转成char*数组返回
  3. switch 可以是int,char,不能是string

第二题:Leetcode239.滑动窗口最大值

题解

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        if (nums.empty() || k == 0)
            return {};
        vector<int> ans;
        deque<int> que; // 存储下标
        for (int i = 0; i < k && i < nums.size(); i++) {
            while (!que.empty() && nums[que.back()] <= nums[i])
                que.pop_back();
            que.push_back(i);
        }
        ans.push_back(nums[que.front()]);
        for (int i = k; i < nums.size(); i++) {
            while (!que.empty() && nums[que.back()] <= nums[i])
                que.pop_back();

            que.push_back(i);
            if (que.front() <= i - k)
                que.pop_front();

            ans.push_back(nums[que.front()]);
        }
        return ans;
    }
};

要点

  1. deque常用API:push_back,pop_back(),front(),back(),size()
  2. 思路:对于每个新元素,如果旧元素小于等于新元素,那么可以将旧元素从deque中删除,因此deque为递减
    在这里插入图片描述
  3. 优化方法:ans预先reserve空间,避免push_back反复开辟。

第三题:Leetcode347.前K个高频元素

题解1

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> times;
        priority_queue<pair<int, int>> pq;
        for (auto n : nums)
            times[n]++;
        for (auto it : times)
            pq.push(pair<int, int>(it.second, it.first));

        vector<int> ans;
        for (int i = 0; i < k; i++) {
            ans.push_back(pq.top().second);
            pq.pop();
        }
        return ans;
    }
};

要点

  1. priority_queue默认是大顶堆,pair默认是以first比较;
  2. 对map顺序无要求时,使用unordered_map即可,其查询效率和增删效率均为O(1),如果使用map,则为O(lgn)(set与unordered_set类似)

题解2:使用小顶堆,自己写排序方法

class Solution {
public:
    class mycmp {
    public:
    	//小顶堆(l>r,比较拗)
        bool operator()(const pair<int, int>& l, const pair<int, int>& r) {
            return l.first > r.first;
        }
    };

    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> times;
        for (auto n : nums)
            times[n]++;

        priority_queue<pair<int, int>, vector<pair<int, int>>, mycmp> pq;
        for (auto n : times) {
            pq.push(pair<int, int>(n.second, n.first));
            if (pq.size() > k)
                pq.pop();
        }

        vector<int> ans(k);
        for (int i = k - 1; i >= 0; i--) {
            ans[i] = pq.top().second;
            pq.pop();
        }
        return ans;
    }
};

要点

  1. priority_queue<pair<int, int>, vector<pair<int, int>>, mycmp> pq;使用vector存储,mycmp函数为重载()运算符的比较算子
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值