//逆波兰表达式
class Solution
{
public:
int evalRPN(vector<string>& tokens)
{
stack<int> mystack;//tokens只包含了int和四个表达式则可以用int
if (tokens.empty())
return 0;
for (string s : tokens)
{
if (s == "+" || s == "-" || s == "*" || s == "/")
{
int b = mystack.top(); mystack.pop();
int a = mystack.top(); mystack.pop();
if (s == "+")
mystack.push(a + b);
if (s == "-")
mystack.push(a - b);
if (s == "*")
mystack.push(a * b);
if (s == "/")
mystack.push(a / b);
}
else
{
mystack.push(atoi(s.c_str()));//可直接用stoi(),此处先将string转换为const char*类型再转为int型
}
}
return mystack.top();
}
};
08-15
1239
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)