leetcode刷题——数据结构(3):栈和队列

栈和队列

1. 用栈实现队列

思考后可做出

2. 用队列实现栈

思考后可做出,

解法可简化

3. 最小值栈

思考后可做出

 

4. 用栈实现括号匹配

第一次可做出Ok

 

5. 数组中元素与下一个比它大的元素之间的距离

思考后可做出,

解法可简化

6. 循环数组中比当前元素大的下一个元素

思考后可做出

1. 用栈实现队列

232. Implement Queue using Stacks (Easy)

https://leetcode-cn.com/problems/implement-queue-using-stacks/

栈的顺序为后进先出,而队列的顺序为先进先出。使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序。

class MyQueue {
private:
    stack<int> st1,st2;
public:
    /** Initialize your data structure here. */
    MyQueue() {
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        st1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int val=peek();
        st2.pop();
        return val;
    }
    
    /** Get the front element. */
    int peek() {
        if(st2.empty())
            while(!st1.empty()){
                st2.push(st1.top());
                st1.pop();
            }
        return st2.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return st1.empty()&&st2.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();
 */

2. 用队列实现栈

225. Implement Stack using Queues (Easy)

https://leetcode-cn.com/problems/implement-stack-using-queues/

在将一个元素 x 插入队列时,为了维护原来的后进先出顺序,需要让 x 插入队列首部。而队列的默认插入顺序是队列尾部,因此在将 x 插入队列尾部之后,需要让除了 x 之外的所有元素出队列,再入队列。

一个队列即可实现!!!

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

3. 最小值栈

155. Min Stack (Easy)

https://leetcode-cn.com/problems/min-stack/

可用两个栈实现

4. 用栈实现括号匹配

20. Valid Parentheses (Easy)

https://leetcode-cn.com/problems/valid-parentheses/

5. 数组中元素与下一个比它大的元素之间的距离

739. Daily Temperatures (Medium)

https://leetcode-cn.com/problems/daily-temperatures/

 在遍历数组时用栈把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        vector<int> ret(T.size());
        stack<int> maxT;
        for(int i=T.size()-1;i>=0;i--){
            while(!maxT.empty()&&T[i]>=T[maxT.top()])
                maxT.pop();
            ret[i]=(maxT.empty())?0:(maxT.top()-i);
            maxT.push(i);
        }
        return ret;        
    }
};

6. 循环数组中比当前元素大的下一个元素

503. Next Greater Element II (Medium)

https://leetcode-cn.com/problems/next-greater-element-ii/

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        stack<int> mst;
        vector<int> ret(nums.size());
        for(int i=nums.size()-2;i>=0;i--) mst.push(nums[i]);
        for(int i=nums.size()-1;i>=0;i--){
            while(!mst.empty()&&nums[i]>=mst.top())
                mst.pop();
            ret[i]=mst.empty()?-1:mst.top();
            mst.push(nums[i]);
        }
        return ret;
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值