1 题目
150. 逆波兰表达式求值
剑指 Offer II 036. 后缀表达式
2 实现
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long> s;
for (auto& str:tokens)
{
if (str.find_first_of("+-*\/") == string::npos ||
(str.find_first_of("-") != string::npos && str.length() > 1))
{
s.push(stol(str));
}
else
{
int a = s.top();
s.pop();
int b = s.top();
s.pop();
if (str.compare("+") == 0)
{
s.push(b+a);
}
else if (str.compare("-") == 0)
{
s.push(b-a);
}
else if (str.compare("*") == 0)
{
s.push(b*a);
}
else
{
s.push(b/a);
}
}
}
return s.top();
}
};