class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long> st;
for(const auto& i : tokens) {
if(i == "+" || i == "-" || i == "*" || i == "/") {
int num2 = st.top();st.pop();
int num1 = st.top();st.pop();
if(i == "+") st.push(num1 + num2);
else if(i == "-") st.push(num1 - num2);
else if(i == "*") st.push(num1 * num2);
else if(i == "/") st.push(num1 / num2);
} else st.push(stoll(i));
}
return st.top();
}
};
力扣150. 逆波兰表达式求值
最新推荐文章于 2025-04-28 15:27:03 发布