class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for (int i = 0; i < tokens.size(); i++)
{
if(tokens[i] == "+" || tokens[i] == "-"
|| tokens[i] == "*" || tokens[i] == "/")
{
int right = st.top();
st.pop();
int left = st.top();
st.pop();
if(tokens[i] == "+")
{
st.push(left + right);
}
else if(tokens[i] == "-")
{
st.push(left - right);
}
else if(tokens[i] == "*")
{
st.push(left * right);
}
else if(tokens[i] == "/")
{
st.push(left / right);
}
}
else
{
st.push(stoi(tokens[i]));
}
}
return st.top();
}
};
【c++】leetcode150. 逆波兰表达式求值
最新推荐文章于 2024-03-20 21:30:00 发布