题目:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200322193821735.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0taSDM0,size_16,color_FFFFFF,t_70)
代码:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> temp;
for(int i=0;i<tokens.size();i++)
{ if(tokens[i].size()==1&&(tokens[i][0]=='+'||tokens[i][0]=='-'||tokens[i][0]=='*'||tokens[i][0]=='/'))
{ int temp1=temp.top();
temp.pop();
int temp2=temp.top();
temp.pop();
if(tokens[i][0]=='+')
temp.push(temp1+temp2);
else if(tokens[i][0]=='-')
temp.push(temp2-temp1);
else if(tokens[i][0]=='*')
temp.push(temp1*temp2);
else
temp.push(temp2/temp1);}
else{
temp.push(atoi(tokens[i].c_str()));
}
}
return temp.top();
}
};
补充:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200322193852141.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0taSDM0,size_16,color_FFFFFF,t_70)