My c++ 16ms solution


  • -1
    V
    `enter code here`   class Solution {
    public:
        int calculate(string s) {
            if(!s.length())
            return 0;
            int fir_val(0),sec_val(0),thi_val(0);
            char fir_oper('n'),sec_oper('n');
            for(auto t:s)
            {
                if(t==' ')
                ;
                else
                {
                    if(fir_oper=='n')
                {
                    if(t>='0'&&t<='9')
                    fir_val=10*fir_val+t-'0';
                    else
                   fir_oper=t;
                }
                else if(sec_oper=='n')
                {
                    if(t>='0'&&t<='9')
                    sec_val=10*sec_val+t-'0';
                    else
                    {
                        if(t=='+'||t=='-')
                        {
                            switch(fir_oper)
                            {
                                case '+': fir_val+=sec_val;
                                break;
                                case '-': fir_val-=sec_val;
                                break;
                                case '*': fir_val*=sec_val;
                                break;
                                case '/': fir_val/=sec_val;
                                break;
                                default: break;
                            }
                            fir_oper=t;
                            sec_val=0;
                        }
                        else if(fir_oper=='*'||fir_oper=='/')
                        {
                            fir_val=(fir_oper=='*')?(fir_val*sec_val):(fir_val/sec_val);
                            fir_oper=t;
                            sec_val=0;
                        }
                        else sec_oper=t;
                    }
                }`enter code here`
                else {
                    if(t>='0'&&t<='9')
                    thi_val=10*thi_val+t-'0';
                    else {
                        if(t=='*'||t=='/')
                        {
                        sec_val=(sec_oper=='*')?(sec_val*thi_val):(sec_val/thi_val);
                        sec_oper=t;
                        thi_val=0;
                    }
                    else {
                         sec_val=(sec_oper=='*')?(sec_val*thi_val):(sec_val/thi_val);
                         fir_val=(fir_oper=='+')?(fir_val+sec_val):(fir_val-sec_val);
                         sec_val=0;
                         thi_val=0;
                         sec_oper='n';
                         fir_oper=t;
                    }
                }
            }
            }
            }
            if(sec_oper!='n')
            sec_val=(sec_oper=='*')?(sec_val*thi_val):(sec_val/thi_val);
            switch(fir_oper)
            {
                case '+': fir_val+=sec_val;
                break;
                case '-': fir_val-=sec_val;
                break;
                case '*': fir_val*=sec_val;
                break;
                case '/': fir_val/=sec_val;
                break;
                default: break;
            }
            return fir_val;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.