leetcode 225. Implement Stack using Queues 利用隊列構建棧 ---------- java

來源:互聯網
上載者:User

標籤:and   boolean   ret   imp   turn   native   rom   ons   for   

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 solutionprivate 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(); */

 

leetcode 225. Implement Stack using Queues 利用隊列構建棧 ---------- java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.