You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stack<int> tmp;
while (!ss.empty())
{
tmp.push(ss.top());
ss.pop();
}
ss.push(x);
while (!tmp.empty())
{
ss.push(tmp.top());
tmp.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int x = ss.top();
ss.pop();
return x;
}
/** Get the front element. */
int peek() {
return ss.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return ss.empty();
}
private:
stack<int> ss;
};
The text was updated successfully, but these errors were encountered:
题目描述:
使用栈实现队列的下列操作:
示例:
说明:
解题思路:使用一个中间导数的栈,这个tmp栈用于暂时存放数据,当push时,先把原栈导出清空,把push的元素入栈,然后再把tmp栈放回
C++解题:
The text was updated successfully, but these errors were encountered: