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

文章讲述了如何利用栈和哈希表数据结构解决字符串中的括号匹配问题,以及如何在计算器表达式求值中使用栈处理运算。着重讨论了空栈和特殊情况的处理,以及switchcase在处理不同类型数据时的局限性。

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

在这里插入图片描述
字符串相邻项匹配问题,一般就是考虑用栈的方法来做,其实就是一个简单的栈的应用,这个回溯法那个可能的扩号组合方案的题目还不一样,不需要去把所有可能都输出出来(当然也没那么多可能咯)

class Solution {
public:
    bool isValid(string s) {
        stack<char> matchStack;
        unordered_map<char,char> match_map;
        match_map.insert(pair<char,char>('(',')'));
        match_map.insert(pair<char,char>('[',']'));
        match_map.insert(pair<char,char>('{','}'));
        for(auto i : s){
            if(!matchStack.empty() && match_map.find(matchStack.top()) != match_map.end() && match_map.find(matchStack.top())->second == i){
                matchStack.pop();
            }else{
                matchStack.push(i);
            }
        }
        if(matchStack.empty()){
            return true;
        }
        return false;
    }
};

左右括号的匹配操作使用了哈希表的方法,总体上这道题还是简单的,就是写的时候要注意空栈,空哈希表的情况,避免出错

在这里插入图片描述
同样的,这是一道字符串邻项匹配的问题,和有效的括号这道题很像,用一下栈来做

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> matchStack;
        for(auto i : s){
            if(!matchStack.empty() && matchStack.top() == i){
                matchStack.pop();
            }else{
                matchStack.push(i);
            }
        }
        
        string res = "";
        while(!matchStack.empty()){
            res.push_back(matchStack.top());
            matchStack.pop();
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本科时候写计算器的时候应该是写过类似的东西,把逆波兰的后序算式改成符合计算习惯的中序算式

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> operateNum;
        for(string i : tokens){
            if(i == "*"){
                    int lastNum = operateNum.top();
                    operateNum.pop();
                    int firstNum = operateNum.top();
                    operateNum.pop();
                    operateNum.push(firstNum * lastNum);
                    }else if(i == "+"){
                    int lastNum = operateNum.top();

                    operateNum.pop();

                    int firstNum = operateNum.top();

                    operateNum.pop();

                    operateNum.push(firstNum + lastNum);
                }else if(i == "-"){
            
                    int lastNum = operateNum.top();

                    operateNum.pop();

                    int firstNum = operateNum.top();

                    operateNum.pop();

                    operateNum.push(firstNum - lastNum);
                }else if(i == "/"){
        
                    int lastNum = operateNum.top();

                    operateNum.pop();

                    int firstNum = operateNum.top();

                    operateNum.pop();

                    operateNum.push(firstNum / lastNum);
                    }else{

                    operateNum.push(atoi(i.c_str()));
                    }
            }
            
    
        return operateNum.top();
    }
};

这里一开始想用switch case的写法来替代这一大堆的if else,但是后面发现switch 表达式只能是整型或者可以转换为整型的数据,比如int,short,char这些,如float,double和string无法直接转int,不能充当表达式,算是查漏补缺了,以前一直没注意过这个事情

就酱

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值