Posted on

Description

Submission

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> stk;

        for(auto& token: tokens) {
            if(token == "+" || token == "/" || token == "*" || token == "-"){
                int right = stk.top();
                stk.pop();
                int left = stk.top();
                stk.pop();

                if(token == "+") stk.push(left + right);
                if(token == "-") stk.push(left - right);
                if(token == "*") stk.push(left * right);
                if(token == "/") stk.push(left / right);
            } else {
                stk.push(stoi(token));
            }
        }

        return stk.top();
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *