代码随想录:逆波兰表达式求值
代码随想录:逆波兰表达式求值
switch纯废物,以后别想着用这个
另外c++的强制转换
stoll //string to long long
stoi //string to int
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long> s;
for (string target : tokens) {
if (target == "+" || target == "-" || target == "*" || target == "/") {
long long a = s.top(); s.pop();
long long b = s.top(); s.pop();
if (target == "+") {
s.push(b + a);
} else if (target == "-") {
s.push(b - a);
} else if (target == "*") {
s.push(b * a);
} else if (target == "/") {
s.push(b / a);
}
} else {
s.push(stoll(target));
}
}
return s.top();
}
};