问题描述
使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
解题思路
实现
stackSave:取,查,弹出
stackHelper:存
class MyQueue {
private Stack<Integer> stackSave;//存储队列
private Stack<Integer> stackHelper;//辅助
/** Initialize your data structure here. */
public MyQueue() {
stackSave = new Stack<Integer>();
stackHelper = new Stack<Integer>();
}
/** Push element x to the back of queue. */
public void push(int x) {//在push的时候就把顺序调整好,始终取值的时候都要从stackSave中取
while (!stackSave.empty()) {
stackHelper.push(stackSave.pop());
}
stackSave.push(x);
while (!stackHelper.empty()){
stackSave.push(stackHelper.pop());
}
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {//从stackSave中弹出即可,在push的时候,已经将最终的顺序调整好了
return stackSave.pop();
}
/** Get the front element. */
public int peek() {//从stackSave中,查看最栈顶元素即可,就是队首元素,因为在push的时候队首队尾已经调整好顺序
return stackSave.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stackSave.empty();
}
}
another
要始终保持一个stack为空
class MyQueue {
private Stack<Integer> stackSave;//
private Stack<Integer> stackHelper;//
/** Initialize your data structure here. */
public MyQueue() {
stackSave = new Stack<Integer>();
stackHelper = new Stack<Integer>();
}
/** Push element x to the back of queue. */
public void push(int x) {//所有的存操作都在stackHelper中,存的时候就把另一个stack倒入到stackHelper中,再操作
action(stackSave, stackHelper);
stackHelper.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {//所有的取。弹出都要从stacksave中取,所以第一步要讲stackHelper栈的元素都倒入stacksave中,然后再弹出
action(stackHelper, stackSave);
return stackSave.pop();
}
/** Get the front element. */
public int peek() {//所有的取,查,弹出都要从stacksave中取,所以第一步要讲stackHelper栈的元素都倒入stacksave中,然后再查
action(stackHelper, stackSave);
return stackSave.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {//这时,数据可能都在stackHelper中,也有可能都在stackSave中,要判断一下
return (stackHelper.empty() && stackSave.empty());
}
private void action(Stack<Integer> source, Stack<Integer> dest) {
while (!source.empty()){
dest.push(source.pop());
}
}
}
another
这里属于优化,减少导换stack的次数
class MyQueue {
Stack<Integer> in = null;
Stack<Integer> out = null;
public MyQueue() {
in = new Stack<>();
out = new Stack<>();
}
public void push(int x) {
in.push(x);
}
public int pop() {
peek();
return out.pop();
}
public int peek() {//不空的时候,直接查询返回即可
if(out.isEmpty())//空的时候,再倒换stack
while(!in.isEmpty())
out.push(in.pop());
return out.peek();
}
public boolean empty() {
return in.isEmpty() && out.isEmpty();
}
}
知识点
Java Stack 类
栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
堆栈只定义了默认构造函数,用来创建一个空栈。 堆栈除了包括由Vector定义的所有方法,也定义了自己的一些方法。
Stack()
除了由Vector定义的所有方法,自己也定义了一些方法:
序号 | 方法描述 |
---|---|
1 boolean empty() | 测试堆栈是否为空。 |
2 Object peek( ) | 查看堆栈顶部的对象,但不从堆栈中移除它。 |
3 Object pop( ) | 移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
4 Object push(Object element) | 把项压入堆栈顶部。 |
5 int search(Object element) | 返回对象在堆栈中的位置,以 1 为基数。 |