LeetCode相关典型题解合集——栈和队列

所有的题型目录在下面的链接
LeetCode相关典型题解合集(两百多道题)


232. Implement Queue using Stacks

public:
    stack<int > s1;
    stack<int > s2;  
    /** Initialize your data structure here. */
    MyQueue() {
          
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        //注意要判断s2是否为空
        if(s2.empty()){
            while(!s1.empty()){
                 s2.push(s1.top());
                s1.pop();
             }
        }
        int temp=0;
        if(!s2.empty()){
            temp=s2.top();
            s2.pop();
        }
        return temp;
        
    }
    
    /** Get the front element. */
    int peek() {
        if(s2.empty()){
            while(!s1.empty()){
                 s2.push(s1.top());
                s1.pop();
             }
        }
        int temp=0;
        if(!s2.empty()){
            temp=s2.top();
        }
        return temp;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        if(s1.empty()&&s2.empty()){
            return true;
        }
        return false;
    }

225. Implement Stack using Queues

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

20. Valid Parentheses

思路:遍历到左括号向栈中加入同种类右括号,遍历到右括号时,如果栈顶元素跟自己相同,则表示有跟自己匹配的左括号已经在之前遍历过了,不同则不匹配

bool isValid(string s) {
        //如果字符串s个数不为偶数的话,必定返回false
        if(s.length()%2!=0){
            return false;
        }
        stack<char> res;
        for(int i=0;i<s.length();i++){
            if(s[i]=='('){
                res.push(')');
            }
            else if(s[i]=='{'){
                res.push('}');
            }
            else if(s[i]=='['){
                res.push(']');
            }else{
                if(res.empty()||res.top()!=s[i]){
                    return false;
                }
                res.pop();
            }
        }
        //注意,最后要判断栈是否为空,不为空也是false。比如"(("这种情况
        return res.empty();
    }

739. Daily Temperatures

思路:第一反应肯定是暴力破解法,这里不提倡
所以,用栈来解决,栈里面放温度表的索引值,当后面有比当前的索引值打的时候就要进行栈操作。具体看代码

public:
    vector<int> dailyTemperatures(vector<int>& T) {
        //注意这里面res数目和温度的个数一直
        vector<int> res(T.size());
        stack<int> s;
        for(int i=0;i<T.size();i++){
            while(!s.empty()&&T[i]>T[s.top()]){
                int temp=s.top();
                res[temp]=i-temp;
                s.pop();
            }
            s.push(i);
        }
        return res;
    }

503. Next Greater Element II

跟上面那个题一模一样的思路,维护一个单调栈

public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        
        vector<int> res(nums.size(),-1);
        stack<int> s;
        if(nums.size()==0){
            return res;
        }
        for(int i=0;i<2*nums.size()-1;i++){
            while(!s.empty()&&nums[i%nums.size()]>nums[s.top()]){
                res[s.top()]=nums[i%nums.size()];
                s.pop();
            }
            s.push(i%nums.size());
        }
        return res;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值