题目链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/
C++代码如下:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
for (auto &s : tokens) {
if (s == "+" || s == "-" || s == "*" || s == "/") {
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
if (s == "+") stk.push(a + b);
else if (s == "-") stk.push(b - a);
else if (s == "*") stk.push(a * b);
else if (s == "/") stk.push(b / a);
} else {
stk.push(stoi(s));
}
}
return stk.top();
}
};