代码随想录算法训练营第10-13天|栈,队列,单调队列,优先级队列

LC 232 用栈实现队列

class MyQueue {
    stack<int> stIn, stOut;
public:
    MyQueue() {

    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if(stOut.empty()){
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int res = stOut.top();
        stOut.pop();
        return res;
    }
    
    int peek() {
        int res = this->pop();
        stOut.push(res);
        return res;
    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

LC 225 用队列实现栈

class MyStack {
    queue<int> que;
public:
    MyStack() {

    }
    
    void push(int x) {
        que.push(x);
    }
    
    int pop() {
        int qsize = que.size();
        while(qsize>1&&qsize--){
            que.push(que.front());
            que.pop();
        }
        int res = que.front();
        que.pop();
        return res;
    }
    
    int top() {
        int res = this->pop();
        que.push(res);
        return res;
    }
    
    bool empty() {
        return que.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

LC 20 有效的括号

实现繁了, 应该直接push } ] ) 

class Solution {
public:
    bool isValid(string s) {
        stack<char> schar;
        for(int i=0; i<s.size(); i++){
            if(s[i]=='(' || s[i]=='{' || s[i]=='['){
                schar.push(s[i]);
            }else{
                if(schar.empty()){
                    return false;
                }
                char tmp = schar.top();
                if((s[i]==')' && tmp=='(') || (s[i]=='}' && tmp=='{') || (s[i]==']' && tmp=='[')){
                    schar.pop();
                }else{
                    return false;
                }
            }
        }
        return schar.empty();
    }
};

LC 1047 删除字符串中的所有相邻重复项

(string可以直接作为栈 push_pack/pop_back 操作, 空间复杂度O(1))

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> schar;
        for(int i=0;i<s.size(); i++){
            if(schar.empty() || schar.top() != s[i]){
                schar.push(s[i]);
            }else{
                schar.pop();
            }
        }
        int ssize = schar.size();
        string res(ssize, '0');
        while(ssize--){
            res[ssize] = schar.top();
            schar.pop();
        }
        return res;
    }
};

LC 150 逆波兰表达式求值

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

LC 239 滑动窗口最大值

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        //单调队列用双端队列
        vector<int> res;
        deque<int> ique;
        for(int i=0; i<nums.size(); i++){
            if(i>=k && ique.front()==nums[i-k]){
                ique.pop_front();
            }
            while(!ique.empty() && ique.back() < nums[i]){
                ique.pop_back();
            } 
            if(i>k-2){
                if(ique.empty()) res.push_back(nums[i]);
                else{
                    res.push_back(ique.front());
                } 
            }
            ique.push_back(nums[i]);
        }
        return res;
    }
};

LC 347 前 K 个高频元素

class Solution {
public:
    struct comparator{
        bool operator()(const pair<int, int> a, const pair<int, int> b){
            return a.second>b.second;
        }
    };
    vector<int> topKFrequent(vector<int>& nums, int k) {
        priority_queue<pair<int,int>, vector<pair<int,int>>,comparator> pq;
        unordered_map<int,int> tmap;
        for (auto& v : nums) {
            tmap[v]++;
        }
        for(auto& v: tmap){
            pq.push(v);
            if(pq.size()>k){
                pq.pop();
            }
        }
        vector<int> res(k);
        while (k--){
            res[k] = pq.top().first;
            pq.pop();
        }
        return res;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值