[stack] C++ 9ms easy to understand


  • 0
        int evalRPN(vector<string>& tokens) {
            stack<int>s;
            for(int i=0;i<tokens.size();i++){
                if(tokens[i]!="+"&&tokens[i]!="-"&&tokens[i]!="*"&&tokens[i]!="/")
                    s.push(stoi(tokens[i]));
                else{
                    int a=s.top();
                    s.pop();
                    if(tokens[i]=="+")
                        s.top()+=a;
                    else if(tokens[i]=="-")
                        s.top()-=a;
                    else if(tokens[i]=="*")
                        s.top()*=a;
                    else if(tokens[i]=="/")
                        s.top()/=a;
                }
            }
            return s.top();
        }
    

Log in to reply
 

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