My answer is pretty pretty simple!!


  • 0
    P
    stack<string> evalStack;
    
    int eval() {
        string t = evalStack.top();
        evalStack.pop();
        if (t == "+")
            return eval() + eval();
        if (t == "-") {
            int second = eval(), first = eval();
            return first - second;
        }
        if (t == "*")
            return eval() * eval();
        if (t == "/") {
            int second = eval(), first = eval();
            return first / second;
        }
        return stoi(t);
    }
    
    int evalRPN(vector<string>& tokens) {
        for (auto s: tokens)
            evalStack.push(s);
        return eval();
    }

Log in to reply
 

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