LeetCode232. 用栈实现队列
https://leetcode-cn.com/problems/implement-queue-using-stacks/
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
- void push(int x) 将元素 x 推到队列的末尾
- int pop() 从队列的开头移除并返回元素
- int peek() 返回队列开头的元素
- boolean empty() 如果队列为空,返回 true ;否则,返回 false
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
思路
需要使用两个栈:inStack和outStack
在push时,只需将数据放进inStack即可。
在pop时,如果outStack为空,就把inStack数据全部导入outStack,再从outStack弹出数据;如果outStack不为空,则直接从outStack弹出数据即可。
如果inStack和outStack都为空的话,那么说明模拟的队列为空。
代码
class MyQueue {
public:
stack<int> inStack;
stack<int> outStack;
/** Initialize your data structure here. */
MyQueue() {}
/** Push element x to the back of queue. */
void push(int x)
{
inStack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop()
{
if (outStack.empty())
{
while (!inStack.empty())
{
int x = inStack.top();
inStack.pop();
outStack.push(x);
}
}
int result = outStack.top();
outStack.pop();
return result;
}
/** Get the front element. */
int peek()
{
int result = this->pop();
outStack.push(result);
return result;
}
/** Returns whether the queue is empty. */
bool empty()
{
return inStack.empty() && outStack.empty();
}
};
/**
* 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();
*/
LeetCode225. 用队列实现栈
https://leetcode-cn.com/problems/implement-stack-using-queues/
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
- void push(int x) 将元素 x 压入栈顶。
- int pop() 移除并返回栈顶元素。
- int top() 返回栈顶元素。
- boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
思路
需要使用两个栈:que1和que2。que2起到备份的作用。
在push时,只需将数据放进que1即可。
在pop时,把que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1。
代码
class MyStack {
public:
queue<int> que1;
queue<int> que2; //起备份作用
/** Initialize your data structure here. */
MyStack() {}
/** Push element x onto stack. */
void push(int x)
{
que1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop()
{
int sz = que1.size();
--sz;
while (sz--)
{
que2.push(que1.front());
que1.pop();
}
int result = que1.front();
que1.pop();
que1 = que2;
while (!que2.empty())
{
que2.pop();
}
return result;
}
/** Get the top element. */
int top()
{
return que1.back();
}
/** Returns whether the stack is empty. */
bool empty()
{
return que1.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/