标签:code ted pre obj hat instant color returns span
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { std::stack<int> aux; while(!_data.empty()) { aux.push(_data.top()); _data.pop(); } aux.push(x); while(!aux.empty()) { _data.push(aux.top()); aux.pop(); } } /** Removes the element from in front of queue and returns that element. */ int pop() { int x = _data.top(); _data.pop(); return x; } /** Get the front element. */ int peek() { return _data.top(); } /** Returns whether the queue is empty. */ bool empty() { return _data.empty(); } private: std::stack<int> _data; }; /** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * bool param_4 = obj.empty(); */
Leetcode 232. Implement Queue using Stacks
标签:code ted pre obj hat instant color returns span
原文地址:https://www.cnblogs.com/randyniu/p/9309462.html