栈和队列面试题练习(力扣leetcode、牛客)

1. 括号匹配问题

class Solution {
public:
    bool isValid(string s) 
    {
        stack<char> ch;
        for(auto e:s)
        {
            if(e=='(' || e=='{' || e=='[')    
                ch.push(e);
            else 
            {
                if(ch.empty() || e==')' 
                   && ch.top()!='(' || e==']' 
                   && ch.top()!='[' || e=='}' 
                   && ch.top()!='{')
                    return false;
                else
                    ch.pop();
            }
        }
        return ch.empty();
    }
};

2. 用队列实现栈

class MyStack {
public:
    queue<int> q;
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        q.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size=q.size()-1;
        for(int i=0;i<size;i++)
        {
            int data=q.front();
            q.pop();
            q.push(data);
        }
        int d=q.front();
        q.pop();
        return d;
    }
    
    /** Get the top element. */
    int top() {
        int size=q.size()-1;
        for(int i=0;i<size;i++)
        {
            int data=q.front();
            q.pop();
            q.push(data);
        }
        int d=q.front();
        q.pop();
        q.push(d);
        return d;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return q.empty();
    }
};

3. 用栈实现队列

class MyQueue {
public:
    stack<int> left;
    stack<int> right;
    /** Initialize your data structure here. */
    MyQueue() {
    }
    
    /** Push element x to the back of queue. */
    void push(int x) 
    {
        right.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() 
    {
        if(left.empty())
        {
            int size=right.size();
            for(int i=0;i<size;i++)
            {
                int d=right.top();
                right.pop();
                left.push(d);
            }
        }
        
        int v=left.top();
        left.pop();
        return v;
    }
    
    /** Get the front element. */
    int peek() 
    {
        if(left.empty())
        {
            int size=right.size();
            for(int i=0;i<size;i++)
            {
                int d=right.top();
                right.pop();
                left.push(d);
            }
        }
        int v=left.top();
        return v;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() 
    {
        return left.empty()&&right.empty();
    }
};

4. 实现一个最小栈

class MinStack {
public:
    stack<int> nors;
    stack<int> mins;
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        nors.push(x);
        if(mins.empty()){
            mins.push(x);
        }
        else
        {
            if(x <=mins.top())
                mins.push(x);
        }
    }
    
    void pop() {
        if(nors.top() == mins.top()){
            mins.pop();
        }
        nors.pop();
    }
    
    int top() {
        return nors.top();
    }
    
    int getMin() {
        return mins.top();
    }
};

5. 设计循环队列

class MyCircularQueue {
public:
    int *array;//数组实现
    int front;
    int end;
    int maxsize;
    MyCircularQueue(int k) {
        array=new int[k+1];
        maxsize = k+1; //此处必须加1不然装不满k个元素
        front = end = 0;
    }
    bool enQueue(int value) {
        if(isFull()) 
        return false;
        else 
        {
            array[end] = value;
            end = (end+1)%maxsize;
            return true;
        }
    }
    bool deQueue() {
        if(isEmpty()) 
        	return false;
        else 
        {
            front=(front+1)%maxsize;
            return true;
        }
    }
    int Front() {
        if(isEmpty()) 
        	return -1;
        return array[front];
    }
    int Rear() {
        if(isEmpty()) 
        	return -1;
        return array[(end-1+maxsize)%maxsize];
    }
    bool isEmpty() {
        if(front==end) 
        	return true;
        else return false;
    }
    bool isFull() {
        if((end+1)%maxsize==front) 
        	return true;
        else return false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值