标签:
问题描述:
Implement the following operations of a stack using queues.
Notes:
push to back, peek/pop from front, size, and is empty operations are valid.link: https://leetcode.com/problems/implement-stack-using-queues/
解决思路:
<思路1>
假设有两个队列,分别为q1和q2. 对数据元素进行操作时使用q1,将q2作为辅助的数据缓存的队列。具体做法如下:
(1) push时,直接将需要push的元素加到q1中;
(2) pop时,首先检查q1是否为空,如果不为空则将q1中的元素都poll到q2中,直到q1中的元素剩余一个为止,剩下的这个元素即为需要pop的元素,poll即可;
(3) peek时,与(2)中的pop类似,区别在于最后不poll,保留该值,最终返回即可。
(4) isEmpty()只需要检查q1.size()是否为0;
<代码>
class MyStack {
private Queue<Integer> q1;
private Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<Integer>();
q2 = new LinkedList<Integer>();
}
// Push element x onto stack.
public void push(int x) {
q1.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
if (q1.size() == 0) {
return ;
}
while (q1.size() > 1) {
q2.offer(q1.poll());
}
q1.poll();
while (q2.size() > 0) {
q1.offer(q2.poll());
}
}
// Get the top element.
public int top() {
if (q1.size() == 0) {
return -1;
}
while (q1.size() > 1) {
q2.offer(q1.poll());
}
int res = q1.poll();
q2.offer(res);
while (q2.size() > 0) {
q1.offer(q2.poll());
}
return res;
}
// Return whether the stack is empty.
public boolean empty() {
return q1.size() == 0;
}
}
<思路2>
思路1中每次pop操作之后,都要将q2中的元素倒回q1中,这样做的的优点可以使得push操作更简单,但是缺点在于效率不高。下面考虑另一种思路,使用两个指针pushTmp和tmp来标记进出栈操作对应的队列和中间缓存的队列,只要这两种类型的队列确定了,对应的pop和push操作和思路1中的是一样的。tmp对应的队列通常为空。
<代码>
class MyStack {
private Queue<Integer> q1;
private Queue<Integer> q2;
private Queue<Integer> pushTmp;
private Queue<Integer> tmp;
public MyStack() {
q1 = new LinkedList<Integer>();
q2 = new LinkedList<Integer>();
}
// Push element x onto stack.
public void push(int x) {
if (q1.size() == 0) {
pushTmp = q2;
} else {
pushTmp = q1;
}
pushTmp.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
if (q1.size() == 0) {
tmp = q1;
pushTmp = q2;
} else {
tmp = q2;
pushTmp = q1;
}
if (pushTmp.size() == 0) {
return ;
}
while (pushTmp.size() > 1) {
tmp.offer(pushTmp.poll());
}
pushTmp.poll();
}
// Get the top element.
public int top() {
if (q1.size() == 0) {
tmp = q1;
pushTmp = q2;
} else {
tmp = q2;
pushTmp = q1;
}
if (pushTmp.size() == 0) {
return -1;
}
while (pushTmp.size() > 1) {
tmp.offer(pushTmp.poll());
}
int res = pushTmp.poll();
tmp.offer(res);
return res;
}
// Return whether the stack is empty.
public boolean empty() {
return q1.size() == 0 && q2.size() == 0;
}
}
拓展问题:两个栈实现一个队列
标签:
原文地址:http://www.cnblogs.com/harrygogo/p/4580114.html