class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> q;
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]!="+"&&tokens[i]!="/"&&tokens[i]!="*"&&tokens[i]!="-")
{
q.push(stoi(tokens[i]));
}
else {
int a, b;
a = q.top();
q.pop();
b = q.top();
q.pop();
if (tokens[i][0] == '*') {
q.push(a * b);
}
else if (tokens[i][0] == '/') {
q.push(b / a);
}
else if (tokens[i][0] == '-') {
q.push(b - a);
}
else {
q.push(b + a);
}
}
}
return q.top();
}
};
150. Evaluate Reverse Polish Notation
最新推荐文章于 2016-05-04 15:22:06 发布