栈与队列总结1

栈与队列总结1

1.有效的括号

https://leetcode-cn.com/problems/valid-parentheses/
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合

在这里插入图片描述

class Solution {
public:
    bool isValid(string s) {
        int n = s.length();
        if(n==0 || n%2!=0) return false;
        unordered_map<char,char> pairs = {
            {')','('},
            {']','['},
            {'}','{'}
        };
        stack<char> stk;
        for(char ch:s){
            if(pairs.count(ch)){
                if(stk.empty() || stk.top()!=pairs[ch]){
                    return false;
                }
                stk.pop();
            }
            else{
                stk.push(ch);
            }
        }
        return stk.empty();
    }
};
  • 时间复杂度:O(n),其中 n 是字符串 s 的长度。
  • 空间复杂度:O(n+∣Σ∣),其中 Σ 表示字符集,本题中字符串只包含 6种括号,∣Σ∣=6。栈中的字符数量为 O(n),而哈希表使用的空间为 O(∣Σ∣),相加即可得到总空间复杂度。

利用栈和哈希表

2.删除最外层的括号

https://leetcode-cn.com/problems/remove-outermost-parentheses/在这里插入图片描述在这里插入图片描述

class Solution {
public:
    string removeOuterParentheses(string S) {
        string res;
        int n = S.length();
        stack<char> stk;
        int left=0;
        for(int i=0;i<n;i++){
            if(S[i]==')'){
                stk.pop();
                if(!stk.empty()){
                    res.push_back(S[i]);
                }else{
                    left = i+1;
                }
            }else{ 
                stk.push(S[i]);
                if(i!=left){
                    res.push_back(S[i]);
                }
            }
        }
        return res;
    }
};
  • 时间复杂度 o(n)
  • 空间复杂度 o(n)
    注:c++ 中 string 本身就提供【入栈】和【出栈】的接口
    push_back ------------push
    pop_back---------------pop
    back----------------------top
3.最小栈

https://leetcode-cn.com/problems/min-stack/
在这里插入图片描述
在这里插入图片描述

class MinStack {
public:
    /** initialize your data structure here. */
    stack<int> x_stack;
    stack<int> min_stack;
    MinStack() {
        min_stack.push(INT_MAX);

    }
    
    void push(int val) {
        x_stack.push(val);
        if(val<min_stack.top()){
            min_stack.push(val);
        }else{
            min_stack.push(min_stack.top());
        }

    }
    
    void pop() {
        x_stack.pop();
        min_stack.pop();
    }
    
    int top() {
        return x_stack.top();
    }
    
    int getMin() {
        return min_stack.top();
    }
};
  • 时间复杂度 o(1)
  • 空间复杂度 o(n)
    需要借助一个辅助栈,辅助栈构建的细节要很懂
4.下一个更大元素I

https://leetcode-cn.com/problems/next-greater-element-i/
在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> res;
        if(nums1.size()==0 || nums2.size()==0) return res;
        //pairs 中存储的是数,以及其对应的下一个更大的值
        unordered_map<int,int> pairs;
        stack<int> stk;
        for(int i=0;i<nums2.size();i++){
            while(!stk.empty() && nums2[i]>stk.top()){
                pairs[stk.top()] = nums2[i];
                stk.pop();
            }
            stk.push(nums2[i]);
        }
        while(!stk.empty()){
            pairs[stk.top()]=-1;
            stk.pop();
        }
        for(int i=0;i<nums1.size();i++){
            res.push_back(pairs[nums1[i]]);
        }
        return res;
    }
};
  • 时间复杂度 o(m+n)
  • 空间复杂度 o(n) -----栈的构建
    借助了哈希表,以及构建栈存储下一个更大元素的方法要懂
5.滑动窗口的最大值

剑指offer 59
https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
在这里插入图片描述
你可以假设 k 总是有效的,在输入数组不为空的情况下,1 ≤ k ≤ 输入数组的大小。

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();
        vector<int> res;
        if(n==0) return res;
        //双端队列
        deque<int> q;
        for(int i=0;i<k;i++){
            int a = 0;
            while(!q.empty() && q.back()<nums[i]){
                q.pop_back();
                a++;
            }
            for(int j=0;j<a;j++){
                q.push_back(nums[i]);
            }
            q.push_back(nums[i]);
        }
        res.push_back(q.front());
        q.pop_front();

        for(int i=k;i<n;i++){
            int a = 0;
            while(!q.empty() && q.back()<nums[i]){
                q.pop_back();
                a++;
            }
            for(int j=0;j<a;j++){
                q.push_back(nums[i]);
            }
            q.push_back(nums[i]);
            res.push_back(q.front());
            q.pop_front();
        }
        return res;

    }
};
  • 时间复杂度 o(n*k)
  • 空间复杂度 o(k)
    借助了双端队列deque进行求解,要理解双端队列里值的变化过程
6.化栈为队 (面试题,剑指offer题)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值