今日任务
- 理论基础
- 232.用栈实现队列
-
- 用队列实现栈
232.用栈实现队列
题目链接:
https://leetcode.cn/problems/implement-queue-using-stacks/description/
题目描述:
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["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
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、peek
和empty
- 假设所有操作都是有效的 (例如,一个空的队列不会调用
pop
或者peek
操作)
进阶:
- 你能否实现每个操作均摊时间复杂度为
O(1)
的队列?换句话说,执行n
个操作的总时间复杂度为O(n)
,即使其中一个操作可能花费较长时间。
题解代码:
class MyQueue {
public:
//二刷
stack<int> stIn;//入栈
stack<int> stOut;//出栈
MyQueue(){
}
//实现元素入队的功能
void push(int x){
stIn.push(x);
}
//实现元素出队的功能
int pop(){
//先将入栈中的所有元素移动到出栈中
if(stOut.empty()){
while(!stIn.empty()){
stOut.push(stIn.top());
stIn.pop();
}
}
int res = stOut.top();
stOut.pop();
return res;
}
//查看队头的元素
int peek(){
int res = this->pop();
stOut.push(res);
return res;
}
//实现判断队列是否为空的功能
bool empty(){
return stIn.empty()&&stOut.empty();
}
//一刷
/*
stack<int> stIn; //入栈,功能是让元素进入
stack<int> stOut;//出栈,功能是模拟队列的出队
MyQueue() {
}
void push(int x) {
stIn.push(x);
}
int pop() {
//当出栈为空时,从入栈中导入入栈中的所有元素
if(stOut.empty()){
// 从stIn导入数据直到stIn为空
while(!stIn.empty()){
stOut.push(stIn.top());
stIn.pop();
}
}
int res = stOut.top();
stOut.pop();
return res;
}
//peek,就是看一下元素
int peek() {
// 直接使用已有的pop函数
int result = this->pop();
// 因为pop函数弹出了元素res,所以再添加回去
stOut.push(result);
return result;
}
//如果队列为空,返回 true ;否则,返回 false
bool empty() {
return stIn.empty() && stOut.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();
*/
225. 用队列实现栈
题目链接:
https://leetcode.cn/problems/implement-stack-using-queues/description/
题目描述:
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push
、top
、pop
和 empty
)。
实现 MyStack
类:
void push(int x)
将元素 x 压入栈顶。int pop()
移除并返回栈顶元素。int top()
返回栈顶元素。boolean empty()
如果栈是空的,返回true
;否则,返回false
。
注意:
- 你只能使用队列的基本操作 —— 也就是
push to back
、peek/pop from front
、size
和is empty
这些操作。 - 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
["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
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、top
和empty
- 每次调用
pop
和top
都保证栈不为空
**进阶:**你能否仅用一个队列来实现栈。
题解代码:
class MyStack {
public:
//二刷
//用队列实现栈,需要用到一个队列
queue<int> q;
MyStack(){
}
//实现元素入栈的功能
void push(int x){
q.push(x);
}
//实现元素出栈的功能
int pop(){
int size = q.size();
size -= 1;
while(size){ //将size-1个元素重新入队,就会实现像栈一样的后进先出功能
int front = q.front();
q.push(front);
q.pop();
size--;
}
int res = q.front();
q.pop();
return res;
}
//返回栈顶元素
int top(){
return q.back();
}
//实现判断栈空与否地功能
bool empty(){
return q.empty();
}
//一刷
/*
//实现栈所需的队列
queue<int> q;
MyStack() {
}
//将元素 x 压入栈顶。
void push(int x) {
q.push(x);
}
// 移除并返回栈顶元素。
int pop() {
//获取辅助队列q的元素个数
int size = q.size();
size -= 1; //我们只将size-1个元素弹出,再让它们重新入队
while(size){
//获取对头元素
int front = q.front();
//将对头的这个元素重新入队
q.push(front);
//已经将元素重新放入队尾了,所以将对头的这个front删除
q.pop();
//我们已经处理了一个元素,要处理的(要重新入队的)元素个数减1
size--;
}
int res = q.front();
q.pop();
return res;
}
// 返回栈顶元素。
int top() {
//要栈顶的元素,其实就是要我辅助队列中back位置的元素
return q.back();
}
//如果栈是空的,返回 true ;否则,返回 false 。
bool empty() {
return q.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();
*/