Created
July 19, 2022 13:59
-
-
Save icameling/cabbf830a02cf3a86e8088c4c13afc56 to your computer and use it in GitHub Desktop.
#栈与队列 #逆波兰表达式求值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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