leetcode刷题栈与队列

leetcode20:有效的括号

class Solution{
    public:
        bool isValid(string s)
        {
            stack<char> st;
            for(char c : s)
            {
                if(c == '{')//当碰到的是三种左括号,走前三个if,添加进对应的右括号;
                    st.push('}');//目的是为了后面配对的时候只需判断top和c是否相等即可;
                else if(c == '(')
                    st.push(')');
                else if(c == '[')
                    st.push(']');
                else if(st.empty() || st.pop() != c)//碰到右括号走这里;
                    return false;//如果碰到右括号但里面的左括号已经出栈完全,也是false;
                else
                    st.pop();
            }
            return st.empty();
        }
};

leetcode1047:删除字符串中相邻相等元素

解法一:定义一个栈

class Solution{
    public:
        string removeDuplicates(string s)
        {
            stack<char> st;
            string result;
            for(char c : s)
            {
                if(st.empty() || st.top() != c)//当栈为空或者栈顶元素与当前遍历元素不等
                    st.push(c);//元素可以进栈;
                else
                    st.pop();//否则,则说明相邻元素相同了,栈顶元素弹出;
            }
            while(!st.empty())
            {
                result += st.top();//将栈内所有元素全部弹出,并进行拼接;
                st.pop();
            }
            reverse(result.begin(), result.end());//由于先进后出的特性,要进行一次翻转;
            return result;
        }
};

解法二:用string本身作为栈;

class Solution{
    public:
        string removeDuplicates(string s)
        {
            string result;
            for(char c : s)
            {
                if(result.empty() || result.back() != c)
                    result.push_back(c); 
                else
                    result.pop_back();
            }
            return result;
        }
};

leetcode150:逆波兰表达式

class Solution{
    public:
        int evalRPN(vector<string>& tokens)
        {
            stack<long long>st;
            for(int i = 0; i < tokens.size(); i++)
            {
                if(tokens[i] == "+" || tokens[i] == "-" || 
                    tokens[i] == "*" || tokens[i] == "/")
                {
                    long long num1 = st.top();
                    st.pop();
                    long long num2 = st.top();
                    st.pop();
                    if(tokens[i] == "+")
                        st.push(num2 + num1);
                    if(tokens[i] == "-")
                        st.push(num2 - num1);
                    if(tokens[i] == "*")
                        st.push(num2 * num1);
                    if(tokens[i] == "/")
                        st.push(num2 / num1);
                }
                else
                    st.push(stoll(tokens[i]);//stoll()函数将string转换为long long类型;
            }
                return st.top();
        }
};

Acwing154:滑动窗口

解法一:用数组模拟单调队列

#include <iostream>
const int N = 1000010;

using namespace std;

int a[N], q[N];

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    int hh = 0, tt = -1;//hh代表头指针,tt代表尾指针,初始化为-1和0保证维护的队列为空;
                        //tt初始化为-1也是为了配合后面的++操作;
    for(int i = 0; i < n; i++)
    {
        if(hh <= tt && (i - q[hh] + 1) > k)//当队列不为空,且队列长度超过k时;
            hh++;//头指针++,将队头元素假弹出;
        while(hh <= tt && a[q[tt]] <= a[i])//当队列不为空时,且即将入队的元素比队尾元素大时;
            tt--;//尾指针--,将队尾元素假弹出,用while直到遇到比入队元素还大时停止;
        q[++tt] = i;//单调队列中存的是最大元素的下标;
        if(i >= k - 1)//只要遍历到了k - 1时,代表正好k个元素入队了,此时开始输出最大值;
            printf("%d", a[q[hh]]);
    }

    int hh = 0, tt = -1;
    for(int i = 0; i < n; i++)
    {
        if(hh <= tt && (i - q[hh] + 1) > k)
            hh++;
        while(hh <= tt && a[q[tt]] >= a[i])
            tt--;
        q[++tt] = i;
        if(i >= k - 1)
            printf("%d", a[q[tt]]);
    }
    return 0;
}

解法二:编写单调队列

class Solution{
    private:
        class MyQueue{//单调队列,从大到小;
            public:
                deque<int>queu;
                void pop(int value)
                {
                    if(!que.empty() && value == que.front())
                        que.pop_front();
                }
                void push(int value)
                {
                    while(!que.empty() && value > que.back())
                        que.pop_back();
                    que.push_back(value);
                }
                int front()
                {
                    return que.front();
                }
        }
    public:
        vector<int>MaxSlidingWindow(vector<int>& nums, int k)
        {
            MyQueue que;
            vector<int> result;
            for(int i = 0; i < k; i++)
                que.push(nums[i]);
            result.push_back(que.front());
            for(int i = k; i < nums.size(); i++)
            {
                que.pop(nums[i - k]);
                que.push(nums[i]);
                result.push_back(que.front());
            }
            return result;
        }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值