栈

晚风吻尽荷花叶 11 2025-03-03

一.普通栈

1.1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)

class Solution {
public:
    string removeDuplicates(string s) {
        vector<char> v;
        for(int i = 0;i < s.size();i++)
        {
            //如果栈为空或者要入栈的元素不等于栈顶元素,则入栈
            if(v.empty() || s[i] != v.back())
                v.push_back(s[i]);
            else
                v.pop_back();
        }
        return string(v.begin(),v.end());
    }
};

2.844. 比较含退格的字符串 - 力扣(LeetCode)

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        vector<char> v1,v2;
        for(auto e : s)
        {
            if(e != '#')
                v1.push_back(e);
            else
            {
                if(v1.empty())
                    continue;
                else
                    v1.pop_back();
            }
        }
        for(auto e : t)
        {
            if(e != '#')
                v2.push_back(e);
            else
            {
                if(v2.empty())
                    continue;
                else
                    v2.pop_back();
            }
        }
        return v1 == v2? true : false;
    }
};

3.227. 基本计算器 II - 力扣(LeetCode)

class Solution {
public:
    int calculate(string s) {
        char op = '+';
        vector<int> st;
        int ret = 0;
        for(int i = 0;i < s.size();)
        {
            if(isspace(s[i]))
                i++;
            else if(isdigit(s[i]))
            {
                int tmp = 0;
                while(i < s.size() && isdigit(s[i]))
                    tmp = tmp * 10 + (s[i++] - '0');
                if(op == '+')
                    st.push_back(tmp);
                if(op == '-')
                    st.push_back(-tmp);
                if(op == '*')
                    st.back() *= tmp;
                if(op == '/')
                    st.back() /= tmp;
            }
            else 
            op = s[i++];
        }
        for(auto e : st)
            ret += e;
        return ret;
    }
};

4.394. 字符串解码 - 力扣(LeetCode)

class Solution {
public:
    string decodeString(string s) {
        stack<int> numst;
        stack<string> st;
        st.push("");
        int n = s.size();
        
        for(int i = 0;i < n;)
        {
            if(isdigit(s[i]))
            {
                int tmp = 0;
                while(i < n && isdigit(s[i]))
                    tmp = tmp * 10 + (s[i++] - '0');
                numst.push(tmp);
            }
            else if(s[i] == '[')
            {
                i++;
                string tmp;
                while(islower(s[i]))
                    tmp += s[i++];
                st.push(tmp);
            }
            else if(s[i] == ']')
            {
                int k = numst.top();
                numst.pop();
                string tmp = st.top();
                st.pop();
                while(k--)
                    st.top() += tmp;
                i++;
            }
            else
            {
                while(i < n && islower(s[i]))
                    st.top() += s[i++];
            }
        }
        return st.top();
    }
};

5.946. 验证栈序列 - 力扣(LeetCode)

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> st;
        int i = 0;
        for(auto e : pushed)
        {
            st.push(e);
            while(!st.empty() && st.top() == popped[i])
            {
                st.pop();
                i++;
            }
        }
        return st.empty()? true : false;
    }
};

6.20. 有效的括号 - 力扣(LeetCode)

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

7.155. 最小栈 - 力扣(LeetCode)

class MinStack {
public:
    MinStack() {
        
    }
    
    void push(int val) {
        st.push(val);
        if(minst.empty() || val <= minst.top())
            minst.push(val);
    }
    
    void pop() {
        int n = st.top();
        st.pop();
        if(n == minst.top())
            minst.pop();
    }
    
    int top() {
        return st.top();
    }
    
    int getMin() {
        return minst.top();
    }
    stack<int> minst;
    stack<int> st;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

8.232. 用栈实现队列 - 力扣(LeetCode)

class MyQueue {
public:
    MyQueue() {
        
    }
    
    void push(int x) {
        pushst.push(x);
    }
    
    void transferIfNeeded()
    {
        if(popst.empty())
        {
            while(!pushst.empty())
            {
                popst.push(pushst.top());
                pushst.pop();
            }
        }
    }


    int pop() {
        transferIfNeeded();
        int n = popst.top();
        popst.pop();
        return n;
    }
    
    int peek() {
        transferIfNeeded();
        return popst.top(); 
    }
    
    bool empty() {
        return pushst.empty() && popst.empty();
    }
    stack<int> pushst;
    stack<int> popst;
};

/**
 * 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();
 */

二.单调栈

8.739. 每日温度 - 力扣(LeetCode)

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int> st;
        vector<int> v(temperatures.size(),0);
        for(int i = 0;i < temperatures.size();i++)
        {
            while(!st.empty() && temperatures[i] > temperatures[st.top()])
            {
                v[st.top()] = i - st.top();
                st.pop();
            }
            st.push(i);
        }
        return v;
    }
};

9.496. 下一个更大元素 I - 力扣(LeetCode)

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int> nextGreater;
        stack<int> st;
        vector<int> ret;
        for(auto e : nums2)
        {
            while(!st.empty() && e > st.top())
            {
                int n = st.top();
                st.pop();
                nextGreater[n] = e;
            }
            st.push(e);
        }
        for(auto e : nums1)
            ret.push_back(nextGreater.count(e)? nextGreater[e] : -1);
        return ret;
    }
};