代码随想录day10栈和队列1

发布于:2025-06-22 ⋅ 阅读:(12) ⋅ 点赞:(0)

数组模拟栈

题目链接

#include <iostream>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include<list>
#include <set>
#include <ctime>
#include<unordered_map>
#include <bitset>
#include<random>
#include<regex>
#include <chrono>
#include<unordered_map>
#include<unordered_set>
using namespace std;

typedef long long ll;
#define pr pair<double,int>
#define tp tuple<int,int,int>
const int N = 2e6 + 7;
const int mod = 998244353;
ll stk[N],tt;
// 定义一个名为solve的函数
void solve()
{
    ll m;
    cin>>m;
    while(m--)
    {
        string s;
        ll q;
        cin>>s;
        if(s=="push")
        {
            cin>>q;
            stk[++tt]=q;
        }
        else if(s=="pop")
        {
            tt--;
        }
        else if(s=="query")
        {
            cout<<stk[tt]<<endl;
        }
        else{
            if(tt<=0) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
}

 int main()
{
    cin.tie(0); 
    cout.tie(0);
    ios::sync_with_stdio(false);//提高cin、cout的输入输出效率
    solve();
}

栈的应用 单调栈

题目链接

#include <iostream>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include<list>
#include <set>
#include <ctime>
#include<unordered_map>
#include <bitset>
#include<random>
#include<regex>
#include <chrono>
#include<unordered_map>
#include<unordered_set>
using namespace std;

typedef long long ll;
#define pr pair<double,int>
#define tp tuple<int,int,int>
const int N = 2e6 + 7;
const int mod = 998244353;
ll stk[N],tt;
ll a[N];
// 定义一个名为solve的函数
void solve()
{
    ll m;
    cin>>m;
   for(int i=0;i<m;i++)
   {
       cin>>a[i];
   }
   for(int i=0;i<m;i++)
   {
    while(tt&&stk[tt]>=a[i])
    {
        tt--;
    }
    if(tt<=0) cout<<"-1 ";
    else cout<<stk[tt]<<" ";
    stk[++tt]=a[i];
   }
}

 int main()
{
    cin.tie(0); 
    cout.tie(0);
    ios::sync_with_stdio(false);//提高cin、cout的输入输出效率
    solve();
}

栈(stack)

#include <stack>
//声明
stack<int>stk1;
stack<string>stk2;
stack<ListNode*>stk3;

在这里插入图片描述

myStack.push(10); // 入栈
myStack.pop(); // 出栈
if (!myStack.empty()) {
    int topElement = myStack.top(); // 访问栈顶元素
}
if (myStack.empty()) {
    // 栈为空
}
int stackSize = myStack.size(); // 获取栈的大小

数组模拟队列

题目链接

#include <iostream>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include<list>
#include <set>
#include <ctime>
#include<unordered_map>
#include <bitset>
#include<random>
#include<regex>
#include <chrono>
#include<unordered_map>
#include<unordered_set>
using namespace std;

typedef long long ll;
#define pr pair<double,int>
#define tp tuple<int,int,int>
const int N = 2e6 + 7;
const int mod = 998244353;
ll q[N],tt=-1;//队尾
ll hh;//队头
// 定义一个名为solve的函数
void solve()
{
    ll m;
    while(m--)
    {
        string s;
        cin>>s;
        if(s=="push")
        {
            ll x;
            cin>>x;
            q[++tt]=x;
        }
        else if(s=="pop") hh++;
        else if(s=="query") cout<<q[hh]<<endl;
        else 
        {
            if(tt-hh<=0) cout<<"YES\n";
            else cout<<"NO\n";
        }
    }
}

 int main()
{
    cin.tie(0); 
    cout.tie(0);
    ios::sync_with_stdio(false);//提高cin、cout的输入输出效率
    solve();
}

队列stl(queue)

#include <queue>
std::queue<int> myQueue; // 声明一个空的整数队列

在这里插入图片描述

myQueue.push(10); // 入队
myQueue.pop(); // 出队
if (!myQueue.empty()) {
    int frontElement = myQueue.front(); // 访问队首元素
    int backElement = myQueue.back(); // 访问队尾元素
}
if (myQueue.empty()) {
    // 队列为空
}
int queueSize = myQueue.size(); // 获取队列的大小

双端队列stl(deque)

std::deque<int> myDeque; // 声明一个空的整数双端队列

在C++的STL(Standard Template Library)中,deque(双端队列)是一个非常有用的容器。deque是一个双向开口的连续线性空间,可以在两端进行高效的插入和删除操作。本文将详细介绍C++ STL中deque的特点、使用方法和一些常见操作。
特点
deque具有以下特点:
双向开口:deque可以在两端进行高效的插入和删除操作,即在队首和队尾都可以进行操作。
动态扩展:deque的内部实现使用了分段连续线性空间,可以动态扩展以适应元素的增加。
随机访问:deque支持随机访问,可以通过下标访问元素。

在这里插入图片描述
myDeque.push_back(10); // 在队尾插入元素
myDeque.push_front(20); // 在队头插入元素
myDeque.pop_back(); // 删除队尾元素
myDeque.pop_front(); // 删除队头元素
if (!myDeque.empty()) {
int frontElement = myDeque.front(); // 访问队头元素
int backElement = myDeque.back(); // 访问队尾元素
}
if (myDeque.empty()) {
// 双端队列为空
}
int dequeSize = myDeque.size(); // 获取双端队列的大小
int element = myDeque[2]; // 访问下标为2的元素

滑动窗口单调队列

题目链接

#include <iostream>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include<list>
#include <set>
#include <ctime>
#include<unordered_map>
#include <bitset>
#include<random>
#include<regex>
#include <chrono>
#include<unordered_map>
#include<unordered_set>
using namespace std;

typedef long long ll;
#define pr pair<double,int>
#define tp tuple<int,int,int>
const int N = 2e6 + 7;
const int mod = 998244353;
ll a[N];

// 定义一个名为solve的函数
void solve()
{
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i ++ ) cin >> a[i];//读入数据
    deque<int> q;
    for(int i = 1; i <= n; i++)
    {
        while(q.size() && q.back() > a[i]) //新进入窗口的值小于队尾元素,则队尾出队列
            q.pop_back();
        q.push_back(a[i]);//将新进入的元素入队
        if(i - k >= 1 && q.front() == a[i - k])//若队头是否滑出了窗口,队头出队 
            q.pop_front();
        if(i >= k)//当窗口形成,输出队头对应的值
            cout << q.front() <<" ";
    }
    q.clear();
    cout << endl;

    //最大值亦然
    for(int i = 1; i <= n; i++)
    {
        while(q.size() && q.back() < a[i]) q.pop_back();
        q.push_back(a[i]);
        if(i - k >= 1 && a[i - k] == q.front()) q.pop_front(); 
        if(i >= k) cout << q.front() << " ";

    }
}

 int main()
{
    cin.tie(0); 
    cout.tie(0);
    ios::sync_with_stdio(false);//提高cin、cout的输入输出效率
    solve();
}

232.用栈实现队列

题目链接
文章讲解
主要思路就是把栈倒过来

class MyQueue {
    private:
        stack<int> in,out;
        void in2out() {
        while (!in.empty()) {
            out.push(in.top());
            in.pop();
        }
        }
public:
    MyQueue() {
        
    }
    
    void push(int x) {
        in.push(x);
    }
    
    int pop() {
        if(out.empty()) in2out();
        int x=out.top();
        out.pop();
        return x;
    }
    
    int peek() {
         if(out.empty()) in2out();
        return out.top();
    }
    
    bool empty() {
         if(out.empty()) in2out();
        return out.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 {
    private:
    queue<int> in,out;
    

public:
    MyStack() {
        
    }
    
    void push(int x) {
        out.push(x);
        while(!in.empty())
        {
            out.push(in.front());
            in.pop();
        }
        swap(in,out);
    }
    
    int pop() {
        int x=in.front();;
        in.pop();
        return x;
    }
    
    int top() {
        return in.front();
    }
    
    bool empty() {
        return in.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) {
      
        stack<char> stk;
        for(int i=0;i<s.size();i++)
        {
            if (s[i]=='(')  stk.push(')');
           else if (s[i]=='[')  stk.push(']');
            else if (s[i]=='{')  stk.push('}');
            else{
                if(stk.empty()||stk.top()!=s[i]) return false;
                stk.pop();
            }
        }
       return stk.empty();
    }
};

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

题目链接
文章讲解

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