题解 | #逆波兰表达式求值#
逆波兰表达式求值
https://www.nowcoder.com/practice/885c1db3e39040cbae5cdf59fb0e9382
#include <sys/ucontext.h>
class Solution {
public:
int evalRPN(vector<string>& tokens) {
int num = 0;
stack<int> temp;
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "*") {
num = temp.top();
temp.pop();
temp.top() *= num;
} else if (tokens[i] == "+") {
num = temp.top();
temp.pop();
temp.top() += num;
} else if (tokens[i] == "-") {
num = temp.top();
temp.pop();
temp.top() -= num;
} else if (tokens[i] == "/") {
num = temp.top();
temp.pop();
temp.top() /= num;
} else {
temp.push(stoi(tokens[i]));
}
}
return temp.top();
}
};
