diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index fb2851f1..4412a819 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -292,6 +292,40 @@ class MyStack { } ``` +优化,使用一个 Queue 实现 +```java +class MyStack { + + Queue queue; + + public MyStack() { + queue = new LinkedList<>(); + } + + //每 offer 一个数(A)进来,都重新排列,把这个数(A)放到队列的队首 + public void push(int x) { + queue.offer(x); + int size = queue.size(); + //移动除了 A 的其它数 + while (size-- > 1) + queue.offer(queue.poll()); + } + + public int pop() { + return queue.poll(); + } + + public int top() { + return queue.peek(); + } + + public boolean empty() { + return queue.isEmpty(); + } +} + +``` + Python: ```python