你只能使用栈的标准操作:push to top,peek/pop from top, size 和 is empty; 如果你选择的编程语言没有栈的标准库,你可以使用list或者deque等模拟栈的操作; 输入数据保证合法,例如,在队列为空时,不会进行pop或者peek等操作; 样例 MyQueue queue = new MyQueue();
classMyQueue { public: stack<int> is; stack<int> os; /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ voidpush(int x){ is.push(x); } /** Removes the element from in front of queue and returns that element. */ intpop(){ if (os.empty()) { while (!is.empty()) { os.push(is.top()); is.pop(); } } int r = os.top(); os.pop(); return r; } /** Get the front element. */ intpeek(){ if (os.empty()) { while (!is.empty()) { os.push(is.top()); is.pop(); } } return os.top(); } /** Returns whether the queue is empty. */ boolempty(){ return is.empty() && os.empty(); } };
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * bool param_4 = obj.empty(); */