class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> num;
for (int i = 0; i < tokens.size(); ++i) {
string s = tokens[i];
if (s.size() == 1) {
char ch = s[0];
int tmp = 0;
switch (ch) {
case '+':
tmp = num.top();
num.pop();
num.top() += tmp;
continue;
case '-':
tmp = num.top();
num.pop();
num.top() -= tmp;
continue;
case '*':
tmp = num.top();
num.pop();
num.top() *= tmp;
continue;
case '/':
tmp = num.top();
num.pop();
num.top() /= tmp;
continue;
}
}
num.push(stoi(s));
}
return num.top();
}
};
Leetcode 150. Evaluate Reverse Polish Notation
最新推荐文章于 2024-10-01 05:07:35 发布