【LeetCode & 剑指offer刷题】栈与队列题1:9.1 用队列实现栈(225. Implement Stack using Queues)
【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)
225. Implement Stack using Queues
Implement the following operations of a stack using queues.
-
push(x) -- Push element x onto stack.
-
pop() -- Removes the element on top of the stack.
-
top() -- Get the top element.
-
empty() -- Return whether the stack is empty.
Notes:
-
You must use only standard operations of a queue -- which means only push to back , peek/pop from front , size , and is empty operations are valid.
-
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
-
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
//问题:用队列实现栈
/*
方法一:用两个队列
插入 q1借助辅助队q2在队首插入元素
删除 q1队首元素出队
访问 访问q1队首元素
判断 判断q1是否为空
(Two Queues, push - O(n), pop O(1)
*/
#include <queue>
class
MyStack
{
private
:
queue
<
int
>
q1
,
q2
;
public
:
/** Initialize your data structure here. */
MyStack
()
{
}
/** Push element x onto stack. */
void
push
(
int
x
)
{
while
(!
q1
.
empty
())
//q1转移到q2
{
q2
.
push
(
q1
.
front
());
q1
.
pop
();
}
q1
.
push
(
x
);
//将元素插入到q1中(处于队首位置)
while
(!
q2
.
empty
())
//q2转移回q1
{
q1
.
push
(
q2
.
front
());
q2
.
pop
();
}
}
/** Removes the element on top of the stack and returns that element. */
int
pop
()
{
int
top_element
=
q1
.
front
();
q1
.
pop
();
return
top_element
;
}
/** Get the top element. */
int
top
()
{
return
q1
.
front
();
}
/** Returns whether the stack is empty. */
bool
empty
()
{
return
q1
.
empty
();
}
};
/*
方法二:用两个队列
插入 在queue1中插入
删除 quue1出队,queue2入队,将queue1中除队尾元素外全部转移到queue2中,再删除queue1中元素即可,最后再q2中元素复制过去q1 (主队q1借助辅助队q2将队尾元素删除)
访问 访问queue1队尾元素
判断是否为空 判断queue1是否为空
注意:stack只有top,某次只能访问栈顶元素,而queue有front,back,某次可以访问队首和队尾元素,不过只能删除队首元素
Two Queues, push O(1), pop O(n)
*/
#include <queue>
class
MyStack
{
private
:
queue
<
int
>
q1
,
q2
;
public
:
/** Initialize your data structure here. */
MyStack
()
{
}
/** Push element x onto stack. */
void
push
(
int
x
)
{
q1
.
push
(
x
);
}
/** Removes the element on top of the stack and returns that element. */
int
pop
()
{
int
top_element
=
top
();
while
(
q1.size() > 1
)
//为删除队尾元素,将q1元素除队尾元素外转移到q2
{
q2
.
push
(
q1
.
front
());
q1
.
pop
();
//删除第一个元素
}
q1
.
pop
();
//删除队尾元素
while
(!
q2
.
empty
())
//将元素再转移到q1
{
q1
.
push
(
q2
.
front
());
q2
.
pop
();
}
return
top_element
;
}
/** Get the top element. */
int
top
()
{
return
q1.back(); //返回队尾元素即为栈顶元素
}
/** Returns whether the stack is empty. */
bool
empty
()
{
return
q1
.
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();
*/
//方法三:一个队列
//每次push某值后,将队首元素依次push到后面并pop队首元素,直到该值称为队首新的队首元素
/*
class Stack {
public:
queue<int> que;
// Push element x onto stack.
void push(int x) {
que.push(x);
for(int i=0;i<que.size()-1;++i){
que.push(que.front());
que.pop();
}
}
// Removes the element on top of the stack.
void pop() {
que.pop();
}
// Get the top element.
int top() {
return que.front();
}
// Return whether the stack is empty.
bool empty() {
return que.empty();
}
};
*/