• leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java


    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 only push to backpeek/pop from frontsize, and is 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).

    用队列构成栈,两种做法:1、一个队列实现,push 是O(n),其他是O(1)

    2、两个队列实现,pop是O(n),其他事O(1)

    class MyStack {
    
    //one Queue solution
    private Queue<Integer> q = new LinkedList<Integer>();
    
    // Push element x onto stack.
    public void push(int x) {
        q.add(x);
        for(int i = 1; i < q.size(); i ++) { //rotate the queue to make the tail be the head
            q.add(q.poll());
        }
    }
    
    // Removes the element on top of the stack.
    public void pop() {
        q.poll();
    }
    
    // Get the top element.
    public int top() {
        return q.peek();        
    }
    
    // Return whether the stack is empty.
    public boolean empty() {
        return q.isEmpty();
    }
    }

    2、

    public class MyStack {
    
        private Queue<Integer> queue1;
        private Queue<Integer> queue2;
        private int num;
        
        /** Initialize your data structure here. */
        public MyStack() {
            queue1 = new LinkedList();
            queue2 = new LinkedList();
            num = 1;
        }
        
        /** Push element x onto stack. */
        public void push(int x) {
            if (num == 1){
                queue1.add(x);
            } else {
                queue2.add(x);
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        public int pop() {
            if (num == 1){
                int size = queue1.size();
                for (int i = 0; i < size - 1; i++){
                    queue2.add(queue1.poll());
                }
                num = 2;
                return queue1.poll();
            } else {
                int size = queue2.size();
                for (int i = 0; i < size - 1; i++){
                    queue1.add(queue2.poll());
                }
                num = 1;
                return queue2.poll();
            }
        }
        
        /** Get the top element. */
        public int top() {
            if (num == 1){
                int size = queue1.size();
                for (int i = 0; i < size - 1; i++){
                    queue2.add(queue1.poll());
                }
                int top = queue1.poll();
                queue2.add(top);
                num = 2;
                return top;
            } else {
                int size = queue2.size();
                for (int i = 0; i < size - 1; i++){
                    queue1.add(queue2.poll());
                }
                int top = queue2.poll();
                queue1.add(top);
                num = 1;
                return top;
            }  
        }
        
        /** Returns whether the stack is empty. */
        public boolean empty() {
            if (num == 1){
                return queue1.isEmpty();
            } else {
                return 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();
     */
  • 相关阅读:
    js拖拽效果 javascript实现将元素拖拽如某容器效果demo
    使用 transform3D 造成网页闪动的底层原因剖析
    设置文字垂直 竖向 显示
    文本光标,高亮选中一些出来
    HTMl5的sessionStorage和localStorage
    event 事件兼容性处理 keycode 大全
    收藏个支持进度条与文件拖拽上传的js File Uploader
    three.js 3D效果
    Winform下的地图开发控件(GMap.NET)使用心得
    ASP.NET Forms验证 实现子域名(SubDomain)共享登陆下的缺陷
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/7160139.html
Copyright © 2020-2023  润新知