第六章 栈与队列

本文介绍了如何使用C++编程语言实现栈和队列数据结构,并通过实例展示了如何用它们来解决匹配括号、逆波兰表达式、滑动窗口最大值、前k个高频元素和接雨水等问题。
摘要由CSDN通过智能技术生成

01用两个栈组成一个队列

//2024.2.7
#include <iostream>
#include <stack>
using namespace std;

class Myqueue{
public:
    Myqueue(){}
    ~Myqueue(){}

    void push(int x){
        stacki.push(x);
    }

    int pop(){
        if(stacko.empty()){
            while(!stacki.empty()){
                stacko.push(stacki.top());
                stacki.pop();
            }
        }
        int result = stacko.top();
        stacko.pop();
        return result;
    }

    int peep(){
        int res = this->pop();
        stacko.push(res);
        return res;
    }

    bool empty(){
        return stacki.empty() && stacko.empty();
    }
private:
    stack<int> stacki;
    stack<int> stacko;
};

int main(){
    Myqueue queue;
    queue.push(1);
    queue.push(2);
    queue.push(3);
    for(int i = 0; i < 3; i++){
        cout << queue.pop() << " ";
    }
}

02用两个队列组成一个栈

//2024.2.7
#include <iostream>
#include <queue>
using namespace std;

class Mystack{
public:
    Mystack(){}
    ~Mystack(){}

    void push(int x){
        queue1.push(x);
    }

    int pop(){
        if(queue1.empty()){
            cout << "no data" << endl;
            return;
        }
        int size = queue1.size();
        while(size > 1){
            queue2.push(queue1.front());
            queue1.pop();
            size--;
        }
        int result = queue1.front();
        queue1.pop();
        queue1 = queue2;
        while(!queue2.empty()){
            queue2.pop();
        }
        return result; 
    }

    bool empty(){
        return queue1.empty();
    }

    int top(){
        return queue1.back();
    }

private:
    queue<int> queue1;
    queue<int> queue2;
};

int main(){
    Mystack stack;
    stack.push(1);
    stack.push(2);
    stack.push(3);
    for(int i = 0; i < 3; i++){
        cout << stack.pop() << " ";
    }
}

03用一个队列实现一个栈

#include <iostream>
#include <queue>
using namespace std;

class Mystack{
public:
    Mystack(){}
    ~Mystack(){}

    void push(int x){
        queue.push(x);
    }

    int pop(){
        if(queue.empty()){
            cout << "no data" << endl;
            return -1;
        }
        int size = queue.size();
        while(size > 1){
            queue.push(queue.front());
            queue.pop();
            size--;
        }
        int result = queue.front();
        queue.pop();

        return result;
    }

    bool empty(){
        return queue.empty();
    }

    int top(){
        return queue.back();
    }

private:
    queue<int> queue;    
};

int main(){
    Mystack stack;
    stack.push(1);
    stack.push(2);
    stack.push(3);
    for(int i = 0; i < 3; i++){
        cout << stack.pop() << " ";
    }
}

04匹配括号

//2024.2.8
#include <iostream>
#include <string>
#include <stack>
using namespace std;

bool a(string& s){
    stack<char> stack;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '{' || s[i] == '[' || s[i] == '('){
            stack.push(s[i]);
        }else if(s[i] == '}'){
            if(!stack.empty() && stack.top() == '{'){
                stack.pop();
            }else{
                return false;
            }
        }else if(s[i] == ']'){
            if(!stack.empty() && stack.top() == '['){
                stack.pop();
            }else{
                return false;
            }
        }else if(s[i] == ')'){
            if(!stack.empty() && stack.top() == '('){
                stack.pop();
            }else{
                return false;
            }
        }
    }
    return stack.empty();
}

int main(){
    string s = "{[(a+b)+(c+d)]*e}*f";
    cout << a(s) ? "匹配" : "不匹配";
}

05逆波兰表达式

//2024.2.8
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;

int a(vector<string>& nums){
    stack<int> stack;
    for(int i = 0; i < nums.size(); i++){
        if(nums[i] != "+" && nums[i] != "-" && nums[i] != "*" && nums[i] != "/"){
            stack.push(stoi(nums[i]));
        }else{
            int num2 = stack.top();
            stack.pop();
            int num1 = stack.top();
            stack.pop();
            if(nums[i] == "+"){
                stack.push(num1 + num2);
            }else if(nums[i] == "-"){
                stack.push(num1 - num2);
            }else if(nums[i] == "*"){
                stack.push(num1 * num2);
            }else if(nums[i] == "/"){
                stack.push(num1 / num2);
            }
        } 
    }
    int result = stack.top();
    stack.pop();
    return result;
}

int main(){
    vector<string> nums = {"2","3","+","8","*","5","/","7","-"};
    cout << a(nums);
}

06滑动窗口最大值

//2024.2.8
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;

void print(int num){
    cout << num << " ";
}

//双指针
vector<int> a(vector<int> nums, int k){
    vector<int> result;
    int slow = 0, fast = k - 1;
    int max = nums[0];
    for(; fast < nums.size(); fast++, slow++){
        for(int i = slow; i <= fast; i++){
            if(nums[i] > max){
                max = nums[i];
            }
        }
        result.push_back(max);
        max = nums[fast];
    }
    return result;
}

//单调队列
class MyQuque{
public:
    deque<int> que;
    void pop(int value){
        if(!que.empty() && que.front() == value){
            que.pop_front();
        }
    }

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

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

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

int main(){
    vector<int> nums = {3,4,2,5,1,6,10,8,12};
    vector<int> result = b(nums, 3);
    for_each(result.begin(), result.end(), print);
}

07前k个高频元素

//2024.2.18
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>
using namespace std;

//小顶堆
class mycomparison{
public:
    bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs){
        return lhs.second > rhs.second;
    }
};

vector<int> topK(vector<int>& nums, int k){
    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;
    for(auto it = map.begin(); it != map.end(); it++){
        pri_que.push(*it);
        if(pri_que.size() > k){
            pri_que.pop();
        }
    }
    vector<int> result(k);
    for(int i = 0; i < k; i++){
        result[i] = pri_que.top().first;
        pri_que.pop();
    }
    return result;
}

void print(int value){
    cout << value << " ";
}

int main(){
    vector<int> a = {3,3,3,4,4,5,1,1,2,2,2,3,5,5,5};
    vector<int> result = topK(a, 2);
    for_each(result.begin(), result.end(), print);
}

08接雨水

//2024.2.18
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

//双指针
int trap1(vector<int>& height){
    if(height.size() < 3){
        return 0;
    }
    int result = 0;
    for(int i = 1; i < height.size() - 1; i++){
        int left = 0, right = 0;
        for(int l = 0; l < i; l++){
            left = max(left, height[l]);
        }
        for(int r = i + 1; r < height.size(); r++){
            right = max(right, height[r]);
        }
        left = min(left, right);
        if(left > height[i]){
            result += left - height[i];
        }
    }
    return result;
}

//动态规划
int trap2(vector<int>& height){
    int result = 0;
    vector<int> maxleft(height.size(), 0);
    vector<int> maxright(height.size(), 0);
    maxleft[0] = height[0];
    for(int i = 1; i < height.size() - 1; i++){
        maxleft[i] = max(maxleft[i -1], height[i]);
    }
    maxright[height.size() - 1] = height[height.size() - 1];
    for(int i = height.size() - 2; i > 0; i--){
        maxright[i] = max(maxright[i + 1], height[i]);
    }
    int smaller = 0;
    for(int i = 1; i < height.size() - 1; i++){
        smaller = min(maxleft[i], maxright[i]);
        if(smaller > height[i]){
            result += smaller - height[i];
        }
    }
    return result;
}

//单调栈
int trap3(vector<int>& height){
    int result = 0;
    return result;
}

int main(){
    vector<int> height = {1,0,2,1,3,1,0,1,2,0,1};
    cout << trap2(height);
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值