Evaluate Reverse Polish Notation
https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
// 4 ms (Beats 90%)
// 15.49 MB (Beats 62%)
#include <stack>
#include <string>
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> s;
for (const string& token : tokens) {
if (token.compare("-") == 0) {
const int op1 = s.top(); s.pop();
const int op2 = s.top(); s.pop();
s.push(op2 - op1);
}
else if (token.compare("*") == 0) {
const int op1 = s.top(); s.pop();
const int op2 = s.top(); s.pop();
s.push(op1 * op2);
} else if (token.compare("+") == 0) {
const int op1 = s.top(); s.pop();
const int op2 = s.top(); s.pop();
s.push(op1 + op2);
} else if (token.compare("/") == 0) {
const int op1 = s.top(); s.pop();
const int op2 = s.top(); s.pop();
s.push(op2 / op1);
} else {
s.push(stoi(token));
}
}
return s.top();
}
};