[leetcode_225]Implement Stack using Queues

模拟一个栈,用vector偷懒解决问题。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Stack {
public:
    vector<int> stack;
    // Push element x onto stack.
    void push(int x) {
        stack.push_back(x);
    }

    // Removes the element on top of the stack.
    void pop() {
        stack.pop_back();
    }

    // Get the top element.
    int top() {
        return stack[stack.size() - 1];
    }

    // Return whether the stack is empty.
    bool empty() {
        return stack.empty();        
    }
};
Licensed under CC BY-NC-SA 4.0