Evaluate Reverse Polish Notation Problem & Solution
See the evaluate reverse Polish notation problem on LeetCode.
C++ Solution
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
static const int _=[]{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> s;
for (auto token : tokens) {
if (token == "+" || token == "-" || token == "*" || token == "/") {
int a = s.top(); s.pop();
int b = s.top(); s.pop();
if (token == "+") {
s.push(a + b);
} else if (token == "-") {
s.push(b - a);
} else if (token == "*") {
s.push(a * b);
} else if (token == "/") {
s.push(b / a);
}
} else {
s.push(stoi(token));
}
}
return s.top();
}
};