代码随想录算法训练营第十一天 |20. 有效的括号 150. 逆波兰表达式求值 1047. 删除字符串中的所有相邻重复项

20. 有效的括号

题目链接:https://leetcode.cn/problems/valid-parentheses/description/
文章讲解:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html
视频讲解:https://www.bilibili.com/video/BV1AF411w78g/?vd_source=5287cb6df25410935566fc8f0d951a5a
class Solution {
public:
    bool isValid(string s) {
        stack<char> stack1;
        int len = s.size();
        
        if(len % 2 == 1)
        {
            return false;
        }

        for(int i = 0; i < len ; i++)
        {
            if(s[i] == '(')
            {
                stack1.push(')');
            }
            else if(s[i] == '{')
            {
                stack1.push('}');
            }
            else if(s[i] == '[')
            {
                stack1.push(']');
            }
            //注意先后顺序
            else if(stack1.empty() || stack1.top() != s[i])
            {
                return false;
            }
            else{
                stack1.pop();
            }
        }
        return stack1.empty();
    }
};

1047. 删除字符串中的所有相邻重复项

题目链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
文章讲解:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html
视频讲解:https://www.bilibili.com/video/BV12a411P7mw/
class Solution {
public:
    string removeDuplicates(string S) {
        //用字符串来解决,字符串的尾部是栈的入口,字符串头部是栈的底部
        string result;
        for(char s : S)
        {
            if(result.empty() || result.back() != s)
            {
                result.push_back(s);
            }
            else
            {
                result.pop_back();
            }
        }
        return result;
    }
};

150. 逆波兰表达式求值

题目链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/
文章讲解:https://programmercarl.com/0150.%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%B1%82%E5%80%BC.html
视频讲解:https://www.bilibili.com/video/BV1kd4y1o7on/

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(num1 + num2);
                }
                //注意减法和除法的顺序
                if (tokens[i] == "-") {
                    st.push(num2 - num1);
                }
                if (tokens[i] == "*") {
                    st.push(num1 * num2);
                }
                if (tokens[i] == "/") {
                    st.push(num2 / num1);
                }
            }
            else
            {
                st.push(stoll(tokens[i]));
            }
        }
        int result = st.top();
        st.pop();
        return result;
    }
};
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值