class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int>st;
for(auto ch:tokens){
if(ch=="*"|| ch=="+"|| ch=="-" ||ch=="/"){
// string top=st.top();
int a =st.top();
st.pop();
int b=st.top();
st.pop();
if(ch=="*"){
st.push(a*b);
}
else if(ch=="+"){
st.push(a+b);
}
else if(ch=="/"){
st.push(b/a);
}
else if(ch=="-") st.push(b-a);
}
else {
st.push(stoi(ch));
}
}
return st.top();
}
};
150. 逆波兰表达式求值(中等 栈的使用)
最新推荐文章于 2024-05-24 10:17:25 发布