Skip to content

Instantly share code, notes, and snippets.

@icameling
Created July 19, 2022 13:59
Show Gist options
  • Save icameling/cabbf830a02cf3a86e8088c4c13afc56 to your computer and use it in GitHub Desktop.
Save icameling/cabbf830a02cf3a86e8088c4c13afc56 to your computer and use it in GitHub Desktop.
#栈与队列 #逆波兰表达式求值
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> nums;
for (const auto& op : tokens) {
if (op == "+" || op == "-" || op == "*" || op == "/") {
int rht = nums.top();
nums.pop();
int lft = nums.top();
nums.pop();
if (op == "+") nums.push(lft + rht);
if (op == "-") nums.push(lft - rht);
if (op == "*") nums.push(lft * rht);
if (op == "/") nums.push(lft / rht);
} else {
nums.push(stoi(op));
}
}
return nums.top();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment