Re: [閒聊] 每日LeetCode

作者: wu10200512 (廷廷)   2024-01-30 20:14:21
原本以為要用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];
}
};
作者: Rushia (みけねこ的鼻屎)   2024-01-30 20:22:00
你重複的代碼寫那麼多次不累ㄇ

Links booklink

Contact Us: admin [ a t ] ucptt.com