Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int evalRPN(vector<string>& tokens) {
- stack<int> stk;
- for(auto token:tokens){
- if(token == "+" || token == "-" || token == "*" || token == "/"){
- int op2 = stk.top(); stk.pop();
- int op1 = stk.top(); stk.pop();
- int res;
- if(token == "+") res = op1+op2;
- else if(token == "-") res = op1-op2;
- else if(token == "*") res = op1*op2;
- else if(token == "/") res = op1/op2;
- stk.push(res);
- }
- else{
- int op = stoi(token);
- stk.push(op);
- }
- }
- return stk.top();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment