Open youngyangyang04 opened 5 months ago
public class MyQueue {
Stack<Integer> stIn;
Stack<Integer> stOut;
public MyQueue() {
stIn = new Stack<>();
stOut = new Stack<>();
}
public void push(int x) {
stIn.push(x);
}
public int pop() {
if (stOut.empty()) {
while (!stIn.empty()) {
stOut.push(stIn.pop());
}
}
return stOut.pop();
}
public int peek() {
int res = pop();
stOut.push(res);
return res;
}
public boolean empty() {
return stIn.empty() && stOut.empty();
}
}
请教一下python代码实现的疑问:题目说“你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可”,但类似len(self.stack_in) self.stack_in是否为空这种操作并不是栈的标准操作。 考虑改为各自用self.size_in/self.size_out来记录是不是更规范一些?
https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html