栈与队列||L20. 有效的括号、L1047. 删除字符串中的所有相邻重复项、L150. 逆波兰表达式求值

文章通过三个LeetCode问题展示了栈和队列在解决编程问题中的应用,如有效括号检查、删除字符串中的相邻重复项和逆波兰表达式求值。作者对比了不同解法,强调了简洁算法的重要性,并提供了C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三题训练,加深对栈与队列的影响

http://L20. 有效的括号https://leetcode.cn/problems/valid-parentheses/

一开始自己思考了一种方法,其在力扣上也能跑通的,但代码长度有点长

class Solution {
public:
    bool isValid(string s)
    {
        stack<char>result;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[')
            {
                result.push(s[i]);
            }
            else if (s[i] == ')')
            {
                if (result.empty())
                    return false;
                else if (result.top() == '(')
                    result.pop();
                else
                    return false;
            }
            else if (s[i] == '}')
            {
                if (result.empty())
                    return false;
                else if (result.top() == '{')
                    result.pop();
                else
                    return false;
            }
            else if (s[i] == ']')
            {
                if (result.empty())
                    return false;
                else if (result.top() == '[')
                    result.pop();
                else
                    return false;
            }
        }
        if (!result.empty())
            return false;
        else return true;
    }
};

再看过卡哥代码讲解之后,才发现好的算法能让自己的代码变的漂亮起来,也就是对算法精度的理解,只要遇到左边括号,就插入一个右括号,若遇到一个右括号,判断其是否等于栈顶的右括号,若相等,就将其弹出,若不等则报错,以此反复

class Solution {
public:
    bool isValid(string s) {
        if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求
        stack<char> st;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '(') st.push(')');
            else if (s[i] == '{') st.push('}');
            else if (s[i] == '[') st.push(']');
            // 第三种情况:遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
            // 第二种情况:遍历字符串匹配的过程中,发现栈里没有我们要匹配的字符。所以return false
            else if (st.empty() || st.top() != s[i]) return false;
            else st.pop(); // st.top() 与 s[i]相等,栈弹出元素
        }
        // 第一种情况:此时我们已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false,否则就return true
        return st.empty();
    }
};

知道今天安排的第一题如何写,后面的也就游刃有余,依葫芦画瓢 

http://● 1047. 删除字符串中的所有相邻重复项https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/

class Solution {
public:
    string removeDuplicates(string s) {
        string sol;
        deque<char>result;
        for (int i = 0; i < s.size(); i++)
        {
            if (result.empty() || result.back() != s[i])
                result.push_back(s[i]);
            else if (result.back() == s[i])
            {
                result.pop_back();
            }
        }
        while (!result.empty())
        {
            sol += result.front();
            result.pop_front();
        }
        return sol;
    }
};

 http://150. 逆波兰表达式求值https://leetcode.cn/problems/evaluate-reverse-polish-notation/

本题也一样,只要理解题目要求就好,遇到符号就取两个元素进行运算

class Solution {
public:
    int evalRPN(vector<string>& tokens) 
    {
        stack<long long>temp;
        if (tokens.empty())//如果为空,就返回0
            return 0;
        for (int i = 0; i < tokens.size(); i++)
        {
            if (tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/")
            {
                temp.push(stoll(tokens[i]));
            }
            else 
            {
                long cal1 = temp.top();
                temp.pop();
                long cal2 = temp.top();
                temp.pop();
                if (tokens[i] == "+")
                    temp.push(cal1 + cal2);
                if(tokens[i] == "-")
                    temp.push(cal2 - cal1);
                if(tokens[i] == "*")
                    temp.push(cal2 * cal1);
                if(tokens[i] == "/")
                    temp.push(cal2 / cal1);
            }
        }
        int aaa = temp.top();
        temp.pop();
        return aaa;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值