代码拉取完成,页面将自动刷新
package com.hit.basmath.learn.queue_stack;
import java.util.ArrayDeque;
import java.util.Queue;
/**
* 225. Implement Stack using Queues
* <p>
* Implement the following operations of a stack using queues.
* <p>
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* empty() -- Return whether the stack is empty.
* <p>
* Example:
* <p>
* MyStack stack = new MyStack();
* <p>
* stack.push(1);
* stack.push(2);
* stack.top(); // returns 2
* stack.pop(); // returns 2
* stack.empty(); // returns false
* <p>
* Notes:
* <p>
* 1. You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
* 2. Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
* 3. You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
*/
public class _225 {
class MyStack {
private Queue<Integer> pusher = new ArrayDeque<>();
private Queue<Integer> popper = new ArrayDeque<>();
private void exchange() {
Queue<Integer> temp = pusher;
pusher = popper;
popper = temp;
}
public void push(int x) {
pusher.add(x);
while (!popper.isEmpty()) pusher.add(popper.poll());
exchange();
}
public int pop() {
return !popper.isEmpty() ? popper.poll() : Integer.MAX_VALUE;
}
public int top() {
return !popper.isEmpty() ? popper.peek() : Integer.MAX_VALUE;
}
public boolean empty() {
return popper.isEmpty();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。