代码随想录| day11|栈与队列part02 ● 20. 有效的括号● 1047. 删除字符串中的所有相邻重复项● 150. 逆波兰表达式求值

20. 有效的括号

链接:代码随想录

 阅读答案并二刷。

class Solution {
public:
    bool isValid(string s) {
        int n=s.size();
        int i=0;
        //建立括号栈
         stack<char>sta;
        while(i<n)
        {
            char temp=s[i];
            if(temp=='('||s[i]=='{'||s[i]=='[')
            {
                sta.push(temp);
            }
            else
            {
                if(sta.empty())
                {
                    return false;
                }
                else{
                    char top=sta.top();
                    if((temp==')'&&top=='(')||(temp=='}'&&top=='{')||(temp==']'&&top=='['))
                    {
                        sta.pop();
                    }
                    else
                    {
                        return false;
                    }
                }
                
            }
            i++;

        }
        if(!sta.empty())//-----------------容易落下,这种情况特殊讨论s ="["
        {
            return false;
        }
        return true;

    }
};

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

链接:代码随想录

 大概看了一下,由于是简单题所以没重新做。

class Solution {
//这道题很让人尴尬,我觉得这道题和上一道基本一样,而且总是简单题,简单题不考呜呜呜
public:
    string removeDuplicates(string s) {
        int n=s.size();
        int i=0;
        stack<char>sta;
        while(i<n)
        {
            char tmp=s[i];
            if(!sta.empty() && sta.top()==tmp)
            {
                 sta.pop();
            }
            else
            {
                sta.push(tmp);
            }
            i++;
        }
        string res="";
        while(!sta.empty())
        {
            char ch=sta.top();
            res.append(1,ch);
            sta.pop();
        }
        reverse(res.begin(),res.end());
        return res;

    }
};

 

150. 逆波兰表达式求值

链接:代码随想录

 

 有个字符串转int的函数

把“-11” 转为 -11

注:C++的switch只能接受数值、字符型(不可以字符串)

class Solution {
/*
只需要一个栈,遇到数字就push,遇到符号就pop出两个数字,然后计算,再push回栈
*/
public:
    int evalRPN(vector<string>& s) {
        int n=s.size();
        int i=0;
        stack<int>sta;//答案及所有中间计算结果可以用 32 位 整数表示
        while(i<n)
        {
            if((s[i]!="+")&&(s[i]!="-")&&(s[i]!="*")&&(s[i]!="/"))//是数字push进栈
            {
                sta.push(stoi(s[i]));
            }
            else//不是数字是符号
            {
                if(sta.size()<2)//当前栈中数字小于2,无法进行计算,异常
                {
                    return INT_MAX;
                }
                else
                {
                    int num1=sta.top();
                    sta.pop();
                    int num2=sta.top();
                    sta.pop();
                    if(s[i]=="+")
                    {
                        sta.push(num1+num2);
                    }
                    else if(s[i]=="-")
                    {
                        sta.push(num2-num1);
                    }
                    else if(s[i]=="*")
                    {
                        sta.push(num2*num1);
                    }
                    else{
                        sta.push(num2/num1);
                    }
                }
               

            }
            i++;     
        }
        return sta.top();

    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值