day5 栈与队列

232. 用栈实现队列

class MyQueue {
public:
    stack<int>stIn;
    stack<int>stOut;
    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();
        // if(stIn.empty()){
        //     while(!stOut.empty()){
        //         stIn.push(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();
 */

225. 用队列实现栈

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

    }
    
    void push(int x) {
        que1.push(x);
    }
    
    int pop() {
        int size=que1.size();
        size--;
        while(size--){
            que2.push(que1.front());
            que1.pop();
        }
       int res=que1.front();
       que1.pop();
       while(!que2.empty()){
        que1.push(que2.front());
        que2.pop();
       } 
       return res;

    }
    
    int top() {
        return que1.back();
    }
    
    bool empty() {
        return que1.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();
 */

20. 有效的括号

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

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

class Solution {
public:
    string removeDuplicates(string s) {
        stack<int>st;
        for(int i=0;i<s.size();i++){
            if(st.empty()||s[i]!=st.top()) st.push(s[i]);
            else st.pop();
        }
        string res="";
        while(!st.empty()){
            res+=st.top();
            st.pop();
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

150. 逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        // 力扣修改了后台测试数据,需要用longlong
        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(num2 + num1);
                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. 滑动窗口最大值

单调队列

class Solution {
public:
    class MyQueue{
        public:
            deque<int>que;

            void pop(int val){
                if(!que.empty()&&que.front()==val){
                    que.pop_front();
                }
             }

            void push(int val){
                while(!que.empty()&&que.back()<val){
                    que.pop_back();
                 }
                que.push_back(val);
            }

            int front(){
                return que.front();
            }
    };

    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        MyQueue que;
        vector<int>res;
        for(int i=0;i<k;i++){
            que.push(nums[i]);
        }
        res.push_back(que.front());
        for(int i=k;i<nums.size();i++){
            que.pop(nums[i-k]);
            que.push(nums[i]);
            res.push_back(que.front());
        }
        return res;

    }
};

347. 前 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) {
        //加入map,统计频率
        unordered_map<int,int>map;
        for(int i=0;i<nums.size();i++){
            map[nums[i]]++;
        }

        //创建优先队列
        priority_queue<pair<int,int>,vector<pair<int,int>>,mycomparison> pri_que;

        //用队列对map中统计的进行排序
        for(auto it=map.begin();it!=map.end();it++){
            pri_que.push(*it);
            if(pri_que.size()>k) {
                pri_que.pop();
            }
        }

        //保存队列中的k个元素
        vector<int>res(k);
        for(int i=0;i<k;i++){
            res[i]=pri_que.top().first;
            pri_que.pop();
        }
        return res;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值