nikunjsoni

150

May 25th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int evalRPN(vector<string>& tokens) {
  4.         stack<int> stk;
  5.         for(auto token:tokens){
  6.             if(token == "+" || token == "-" || token  == "*" || token == "/"){
  7.                 int op2 = stk.top(); stk.pop();
  8.                 int op1 = stk.top(); stk.pop();
  9.                 int res;
  10.                 if(token == "+") res = op1+op2;
  11.                 else if(token == "-") res = op1-op2;
  12.                 else if(token == "*") res = op1*op2;
  13.                 else if(token == "/") res = op1/op2;
  14.                 stk.push(res);
  15.             }
  16.             else{
  17.                 int op = stoi(token);
  18.                 stk.push(op);
  19.             }
  20.         }
  21.         return stk.top();
  22.     }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment