04. 剑指offer刷题-队列&栈

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        if(!stack1.empty() && stack2.empty()){
            while(!stack1.empty()){
                stack2.push(stack1.top());
                stack1.pop();
            }
        }
        int res = stack2.top();
        stack2.pop();
        return res;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

class Solution {
public:
    stack<int> sta, sta_min;
    void push(int val) {
        sta.push(val);
        if(sta_min.empty() || val <= sta_min.top()) sta_min.push(val);
    }
    void pop() {
        if(sta.top() == sta_min.top()) sta_min.pop();
        sta.pop();
    }
    int top() {
        return sta.top();
    }
    int min() {
        return sta_min.top();
    }
};

class Solution {
public:
    bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
        
        stack<int> sta;

        int i, j;
        for(i = 0, j = 0; i < pushV.size(); i++){
            sta.push(pushV[i]);
            while(!sta.empty() && sta.top() == popV[j]) {sta.pop(); j++;}
        }

        if(i == j) return true;
        else return false;
    }
};

 

class Solution {
public:
    string ReverseSentence(string str) {
        
        Myreverse(str, 0 , str.size() - 1);

        for(int i = 0, j = 0; j < str.size(); j++){

            if(j == str.size() - 1 || str[j + 1] == ' ') {
                Myreverse(str, i, j);
                i = j + 2;
            }
        }
        return str;
    }

    void Myreverse(string& str, int left, int right){

        while(left < right){
            swap(str[left], str[right]);
            left++; right--;
        }
    }
};

class Myqueue{
public:
    deque<int> deq;

    void push(int val){

        if(deq.empty() || val <= deq.back()) deq.push_back(val);
        else {
            while(!deq.empty() && val > deq.back()) deq.pop_back();
            deq.push_back(val);
        }
    }
    void pop(int val){
        if(!deq.empty() && val == deq.front()) deq.pop_front();
    }

    int top(){
        return deq.front();
    }
};

class Solution {
public:
    vector<int> maxInWindows(vector<int>& num, int size) {

        vector<int> res;
        if(size < 1  || size > num.size()) return res;

        Myqueue que;

        for(int i = 0; i < size; i++) que.push(num[i]);
        res.push_back(que.top());

        for(int i = size; i < num.size(); i++){
            que.push(num[i]);
            que.pop(num[i - size]);
            res.push_back(que.top());
        }
        return res;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值