Leetcode 225 Implement Stack using Queues 使用隊列實現棧

原題地址

https://leetcode.com/problems/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 only push to back, peek/pop from front, size, and is empty operations are valid.
只能使用隊列的標準操作,即:push 在隊尾插入,peek/pop 從隊首彈出,size 獲取隊列大小,empty 是否爲空。

You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
假設所有操作都是合法的(例如,不會在棧爲空時調用pop和top方法)。

解題思路

使用兩個隊列來模擬一個棧。假設有兩個棧A和B,至少保持其中一個爲空。

1. 入棧 push()

當我們插入一個元素x時,將x放入其中爲空的隊列中,然後把另一個隊列中的所有元素按順序放入這個隊列中。如下:

演示 1,2,3,4入棧

(1) push(1)
   --------------       --------------
A:  1                B:               
   --------------       --------------
(2) push(2)
   --------------       --------------
A:                   B:  2,1           
   --------------       --------------
(3) push(3)
   --------------       --------------
A:  3,2,1             B:                
   --------------       --------------
(4) push(4)
   --------------       --------------
A:                   B:  4,3,2,1               
   --------------       --------------

2. 獲取棧頂元素 top() / 出棧 pop()

獲取棧頂元素和出棧操作比較簡單,由於兩個隊列中有一個爲空,我們只需要從不爲空的那個隊列中取出或彈出隊首的元素即可。

(5) top() ---> return 4
   --------------       --------------
A:                   B:  4,3,2,1               
   --------------       --------------
(5) pop()
   --------------       --------------
A:                   B:  3,2,1               
   --------------       --------------

3. 是否爲空 empty()
當兩個隊列同時爲空時,說明棧爲空。

代碼

代碼真的非常簡單

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        if (a.empty()) {
            a.push(x);
            while (!b.empty()) {
                a.push(b.front());
                b.pop();
            }
        } else {
            b.push(x);
            while (!a.empty()) {
                b.push(a.front());
                a.pop();
            }
        }
    }

    // Removes the element on top of the stack.
    void pop() {
        if (a.empty()) b.pop();
        else a.pop();
    }

    // Get the top element.
    int top() {
        return a.empty() ? b.front() : a.front();
    }

    // Return whether the stack is empty.
    bool empty() {
        return a.empty() && b.empty();
    }
private:
    // 兩個隊列模擬一個棧
    queue<int> a;
    queue<int> b;
};

int main() {
    cout << "add [1-100] to stack." << endl;
    Stack s;
    for (int i = 1; i < 101; ++i) s.push(i);
    while (!s.empty()) {
        cout << s.top() << "->";
        s.pop();
    }
    return 0;
}

完整代碼 https://github.com/Orange1991/leetcode/blob/master/225/cpp/main.cpp


2015/8/28

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章