前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 227. Basic Calculator II(后缀表达式)

LeetCode 227. Basic Calculator II(后缀表达式)

作者头像
ShenduCC
发布2022-03-10 16:58:34
2720
发布2022-03-10 16:58:34
举报
文章被收录于专栏:算法修养算法修养

题目

后缀表达式一把嗦。

代码语言:javascript
复制
class Solution {
public:
    int s1[1000005];
    int s2[1000005];
    int s3[1000005];
    int top1;
    int top2;
    int top3;
    int calculate(string s) {
        
        top1=0;top2=0;top3=0;
        string str="";
        for(int i=0;i<s.length();i++)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                str+=s[i];
                continue;
            }
            
            if(str!="")
            {
                s2[top2++] = atoi(str.c_str());
                str="";
            }
            if(s[i]==' ') continue;
            
            if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
            {
                if(top1==0)
                {
                    s1[top1++]=change(s[i]);
                }
                else
                {
                    while(top1>=1&&judge(change2(s1[top1-1]),s[i]))
                    {
                        
                        s2[top2++]=s1[top1-1];
                        top1--;
                    }
                    s1[top1++]=change(s[i]);
                } 
            }
        }
        
        if(str!="")
            s2[top2++]=atoi(str.c_str());
        
        while(top1!=0)
        {
            s2[top2++]=s1[top1-1];
            top1--;
        }
        
        for(int i=0;i<top2;i++)
        {
            if(s2[i]==-1)
            {
                int x =s3[top3-2]/s3[top3-1];
                top3-=2;
                s3[top3++]=x;
            }
            else if(s2[i]==-2)
            {
                int x =s3[top3-2]*s3[top3-1];
                top3-=2;
                s3[top3++]=x;
            }
            
            else if(s2[i]==-3)
            {
                int x =s3[top3-2]-s3[top3-1];
                top3-=2;
                s3[top3++]=x;
            }
            
             else if(s2[i]==-4)
            {
                int x =s3[top3-2]+s3[top3-1];
                top3-=2;
                s3[top3++]=x;
            }
            else
            {
                s3[top3++]=s2[i];
            }
        }
        
        return s3[0];
        
    }
    
    int change(char x)
    {
        if(x=='+')
            return -4;
        else if(x=='-')
            return -3;
        else if(x=='*')
            return -2;
        else
            return -1;
    }
    
    char change2(int x)
    {
        if(x==-1)
            return '/';
        else if(x==-2)
            return '*';
        else if(x==-3)
            return '-';
        else
            return '+';
        
    }
    
    int judge(char x,char y)
    {
        if(x=='+'||x=='-')
        {
            if(y=='+'||y=='-')
                return 1;
            if(y=='*'||y=='/')
                return 0;
        }
        
        if(x=='*'||x=='/')
        {
            if(y=='+'||y=='-')
                return 1;
            if(y=='*'||y=='/')
                return 1;
        }
        
        return 0;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档