Re: [LeetCode] 刷到面試 Grind169 C++已回收
232. Implement Queue using Stacks easy題
這好簡單
剛好開始的時候寫完
趕緊來看
class MyQueue {
stack<int> in;
stack<int> out;
public:
MyQueue() {
}
void push(int x) {
in.push(x);
}
int pop() {
if(out.empty()){
while(!in.empty()){
out.push(in.top());
in.pop();
}
}
int temp = out.top();
out.pop();
return temp;
}
int peek() {
if(out.empty()){
while(!in.empty()){
out.push(in.top());
in.pop();
}
}
return out.top();
}
bool empty() {
if(in.empty()&&out.empty()) return true;
return false;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.157.124 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1679216986.A.5A4.html
→
03/19 17:10,
2年前
, 1F
03/19 17:10, 1F
推
03/19 17:16,
2年前
, 2F
03/19 17:16, 2F
討論串 (同標題文章)
完整討論串 (本文為第 14 之 14 篇):