[LeetCode]Implement Stack using Queues

來源:互聯網
上載者:User

標籤:

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 may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
  • 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 -- which means only push to back, pop from front, size, and is empty operations are valid.

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

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

[LeetCode]

兩種實現方法

1)兩個隊列,當取棧頂時,把全部元素進到還有一個隊列,然後最後出隊列的就是棧頂。pop的操作也是類似的,注意兩個隊列之間的轉換。

2)一個隊列,記錄隊列此時的長度n,出隊列第n次的元素就是棧頂元素。

我們能夠把出隊列的元素再次入隊列,這樣就能夠用一個隊列實現。

同理用兩個棧實現隊列也是類似的做法。

兩個隊列實現:

class Stack {private:    queue<int>temp[2];    int cur = 0;public:    // Push element x onto stack.    void push(int x) {        temp[cur].push(x);    }    // Removes the element on top of the stack.    void pop(void) {        while(temp[cur].size()>1){            int n = temp[cur].front();            temp[cur].pop();            temp[1-cur].push(n);        }        temp[cur].pop();        cur = 1-cur;    }    // Get the top element.    int top(void) {         while(temp[cur].size()>1){            int n = temp[cur].front();            temp[cur].pop();            temp[1-cur].push(n);        }        int ret = temp[cur].front();        temp[cur].pop();        temp[1-cur].push(ret);        cur = 1- cur;        return ret;    }    // Return whether the stack is empty.    bool empty(void) {        return temp[cur].empty();    }};
一個隊列實現:

class Stack {private:    queue<int>temp;public:    // Push element x onto stack.    void push(int x) {        temp.push(x);    }    // Removes the element on top of the stack.    void pop(void) {       int len = temp.size();       while(len>1){           int n = temp.front();           temp.pop();           temp.push(n);           len -- ;       }       temp.pop();    }    // Get the top element.    int top(void) {        int len = temp.size();        while(len>1){            int n = temp.front();            temp.pop();            temp.push(n);            len -- ;        }        int ret = temp.front();        temp.pop();        temp.push(ret);        return ret;    }    // Return whether the stack is empty.    bool empty(void) {        return temp.empty();    }};



[LeetCode]Implement Stack using Queues

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.