→ Rushia: 你重複的代碼寫那麼多次不累ㄇ 01/30 20:22
原本以為要用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];
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.14.7 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1706616863.A.AE8.html