原本以為要用queue就取叫q
寫到一半發現是stack懶得改就繼續q了
class Solution {
public:
int evalRPN(vector<string>& tokens) {
vector<int> q;
for(const string& t:tokens){
if(t=="+"){
int num1 = q.back();
q.pop_back();
int num2 = q.back();
q.pop_back();
q.push_back(num2+num1);
}
else if(t=="-"){
int num1 = q.back();
q.pop_back();
int num2 = q.back();
q.pop_back();
q.push_back(num2-num1);
}
else if(t=="*"){
int num1 = q.back();
q.pop_back();
int num2 = q.back();
q.pop_back();
q.push_back(num2*num1);
}
else if(t=="/"){
int num1 = q.back();
q.pop_back();
int num2 = q.back();
q.pop_back();
q.push_back(num2/num1);
}
else{
q.push_back(stoi(t));
}
}
return q[0];
}
};