zoukankan      html  css  js  c++  java
  • 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();
     */
  • 相关阅读:
    NHibernate从入门到精通系列(3)——第一个NHibernate应用程序
    你真的了解Ioc与AOP吗?(1)
    C#网络编程TCP通信的粘包问题讨论
    开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo
    NHibernate从入门到精通系列(1)——NHibernate概括
    Spring.NET框架简介及模块说明
    C#|.NET从控制反转(依赖注入)想到事件注入 (非AOP)
    MVC3使用Unity实现接口自动注册
    编写更好的C#代码
    解构控制反转(IoC)和依赖注入(DI)
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/7160139.html
Copyright © 2011-2022 走看看