leetcode oj java Implement Stack using Queues

来源:互联网 发布:mac这么下载阿里旺旺 编辑:程序博客网 时间:2024/05/19 19:16

问题描述:

Implement Stack using Queues

Implement the following operations of a stack using queues.

  • 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.
Notes:
  • You must use only standard operations of a queue -- which means onlypush to back,peek/pop from front, size, andis empty operations are valid.
  • 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.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.


思路: 用两个队列实现一个栈 q1 作为专用的队列,q2作为中转站

入栈: 队列的offer() 或者add();

出栈:将q1的前q1.size()-1个依次放入q2中,poll()出q1中剩下的一个,然后再将q2的依次放入q1中。

top   : 如果队列中只有一个元素,取出即可; 如果队列中有多个元素,先将q1.size()-1个依次放入q2中,取出q1中剩下的,作为top值返回,再依次还原。

empty:   队列的isEmpty() 方法,


package leetcode;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    Queue<Integer> q1 = new LinkedList<Integer>();
    Queue<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() {
        int a = q1.size();
        if(a==1){ q1.poll();}
        else{
        for(int i = 0; i<a-1; i++){
            q2.add(q1.peek());
            q1.poll();
        }
        q1.poll();
        int b = a;
        for(int i = 0; i<b-1; i++){
            q1.add(q2.peek());
            q2.poll();
        }}
    }

    // Get the top element.
    public int top() {
        int a = 0;
        if(q1.size()==1){a = q1.peek();}
        else{
        int m = q1.size();
        for(int i = 0; i<m-1; i++){
            q2.add(q1.peek());
            q1.poll();
        }
        a = q1.peek();      
        q2.add(q1.peek());
        q1.poll();
        int n = q2.size();
        for(int i = 0; i<n; i++){
            q1.add(q2.peek());
            q2.poll();
        }}
//        int a = q1.peek();
        return a;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q1.isEmpty();
    }
}


提交的状态:

16 / 16 test cases passed.
Status:

Accepted

Runtime: 110 ms
Submitted: 28 minutes ago



/----------------------------我是华丽丽的分割线--------------------------/

自己编写了主函数测试:

package leetcode;

public class Stack {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyStack a = new MyStack();
        a.push(1);
        a.push(2);
        a.push(3);
        System.out.println(a.top());
        a.pop();
        System.out.println(a.top());

        
    }

}


1 0