1.用栈表示一个队列
思路:使用两个栈表示,一个栈叫push_stack,一个叫pop_stack,我们只往push_stack中加入新数据,只从pop_stack中取出数据。加入数据,没有什么约束条件;取出数据时候,因为队列是先进先出,所以把push_stack中的内容取出放入到pop_stack中就可以了。但是要注意,必须要判断pop_stack为空,才可以往pop_stack中加入pop_stack.push(push_stack.poll())。这样可以做到,在任意时刻,加入一个数,它都会按照队列先进先出的顺序输出出来。
class MyQueue {
Stack<Integer> push_stack = new Stack<>();
Stack<Integer> pop_stack =new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {
push_stack = new Stack<Integer>();
pop_stack = new Stack<Integer>();
}
/** Push element x to the back of queue. */
public void push(int x) {
push_stack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(pop_stack.isEmpty()){
while(!push_stack.isEmpty()){
pop_stack.push(push_stack.pop());
}
}
return pop_stack.pop();
}
/** Get the front element. */
public int peek() {
if(pop_stack.isEmpty()){
while(!push_stack.isEmpty()){
pop_stack.push(push_stack.pop());
}
}
return pop_stack.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return pop_stack.isEmpty()&&push_stack.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
2.用两个队列表示一个栈
思路:
(1)牛客网左神介绍过一种思路,准备两个队列。一个队列queue,一个队列help,进来的数先放queue中,要输出栈顶的时候,将queue中的数从头部依次放入到help队列中,注意queue中留下最后一个数,这个数就是最后进来的数,也就是所谓的栈顶元素,将这个数输出。然后将queue队列和help队列交换(可以想象成交换名字,交换空间),然后继续放数、输出数都可以。
(2)另外一个是在网上看别人解答时候整理的思路:同样准备两个队列,就叫他queue1和queue2。这个核心思路就是,放一个数进来的时候,哪个队列为空,就放哪个队列里面去,在把另一个队列中的数放进这个队列里面去,它其实就是想每次加入一个数,就把顺序给搞好。思路1是每次输出一个数的时候才去搞顺序。
举个例子吧,我自己看着都乱乱的。
比如我们“入栈”顺序依次是2,5,8,3,那么我们通过两个队列想要得到的顺序是3,8,5,2
首先queue1 queue2都为空,代码中默认先判断queue1是否为空
1)queue1.add(2)
2)这时候,只有queue2为空,所以queue2.add(5),然后,queue2.add(queue1.poll())
所以这个时候队列2中为back-2-5-front(back和front此时只为标记头和尾,方便理解)
3)加入8.这个时候queue1为空,queue1.add(8),然后queue1.add(queue2.poll())
所以队列1中为back-2-5-8-front
4)加入3,此时queue2为空,queue2.add(3),queue2.add(queue1.poll())
所以队列2中的顺序为back-2-5-8-3-front
这样就能一直保证顺序啦
下面给出思路2的代码
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
/** Initialize your data structure here. */
public MyStack() {
queue1 =new LinkedList<Integer>();
queue2 =new LinkedList<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
//哪个为空新数放那个里面,然后另一个队列放进来
if(queue1.isEmpty()){
queue1.add(x);
while(!queue2.isEmpty()){
// int tmp=
//queue2.pop();
queue1.add(queue2.poll());
}
}
else{
queue2.add(x);
while(!queue1.isEmpty()){
//int tmp=;
//queue1.pop();
queue2.add(queue1.poll());
}
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
int tmp=0;
if(!queue1.isEmpty()){
tmp= queue1.poll();
}
if(!queue2.isEmpty()){
tmp= queue2.poll();
}
return tmp;
}
/** Get the top element. */
public int top() {//只是拿出来看一下,并不删除释放空间
int tmp=0;
if(!queue1.isEmpty()){
tmp=queue1.peek();
}
// return queue1.peek();
if(!queue2.isEmpty()){
tmp=queue2.peek();
}
//return queue2.peek();
return tmp;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty()&&queue2.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/