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