C++ 代码
class Solution {
public:
stack<int> stk;
void eval(string op) {
auto b = stk.top(); stk.pop();
auto a = stk.top(); stk.pop();
if (op == "+") stk.push(a + b);
else if (op == "-") stk.push(a - b);
else if (op == "*") stk.push( a * b);
else stk.push(a / b);
return;
}
int evalRPN(vector<string>& tokens) {
unordered_set<string> op{"+", "-", "*", "/"};
for (auto & c : tokens) {
if (op.count(c)) eval(c);
else stk.push(stoi(c));
}
return stk.top();
}
};