文章目录
232.用栈实现队列
文章链接:225. 用队列实现栈
视频链接:栈的基本操作! | LeetCode:232.用栈实现队列
状态:考察栈的基本操作
思路
两个栈实现队列,其实思路非常简单。
初始化
先定义两个栈再说。
stack<int> stIn;
stack<int> stOut;
int push()
将元素 x 推到队列的末尾,直接压入stIn`即可
int pop(int x){
stIn.push(x);
}
void pop()
从队列的开头移除并返回元素。两个栈来模拟队列其实就是,一个作为队列入口stIn,一个作为队列出口stout.
所以当依次压入1, 2, 3, 4,存储在stIn = [1, 2, 3, 4],要出队列的时候依次把4,3,2,1存储在stOut=[4, 3, 2,1],再进行出列队的操作,这样就用栈实现了先进先出。但有两个条件一定注意:
- stOut为空是什么情况呢?从stIn里导入数据(导入stIn全部数据)
- stOut不为空呢?说明栈stOut的栈顶就是我们需要的数据(这里有一点绕,但是还是很好理解。详情可看上面的文章)
void pop(){
while (!stOut.empty()){
stOut.push(stIn.top());
stIn.pop();
}
int result = stOut.top();
stOut.pop();
return result;
}
int peek()
返回队列开头元素。队列开头的元素其实存储在stOut中,如果它为空,那么就把stIn的数据导入。
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
return res;
}
直接代码复用this->pop()
boolean empty()
boolean empty()
如果队列为空,返回true
;否则,返回false
- 其实只要两个栈都空,那队列就为空
bool empty() {
return stIn.empty() && stOut.empty();
}
CPP代码
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
if (stOut.empty()) {
// 从stIn导入数据直到stIn为空
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();
}
};
225. 用队列实现栈
文章链接:225. 用队列实现栈
视频链接:队列的基本操作! | LeetCode:225. 用队列实现栈
状态:考察队列的基本操作,这里的逻辑有点那那个,建议两个队列模拟栈和一个队列模拟栈都写一写
思路
两个队列模拟栈
两个队列模拟栈的基本思路就是,进入队列queue1
入口:[3, 2, 1]
:出口。先入队的是1
,我们现在想弹出3
。那就把1
,2
先放到另一个队列queue2
,然后queue1
弹出3
即可,然后想弹出2
,就要1
,2
回到queue1
,然后1到queue2
,再弹出2
。
详细介绍可看上文中给出的文章链接和视频链接
初始化
queue<int> que1;
queue<int> que2; // 辅助队列,用来备份
void push(int x)
void push(int x){
que1.push(x);
}
int pop()
pop的逻辑就跟上文将的一样,但是一定要记得清空que2,为下一次pop做好准备。
int pop(){
int size = que1.size();
size--;
while (size--){ // 将que1 导入que2,但要留下最后一个元素
que2.push(que1.ftonr());
que1.pop();
}
int result = que1.front(); // 留下的最后一个元素就是要返回的值
que1.pop();
que1 = que2; //直接将que2赋值给que1
while (!que2.empty()){ //清空que2
que2.pop();
}
return result;
}
int top()
int top(){
return que1.back();
}
boolean empty()
bool empty(){
return que1.empty();
}
一个队列模拟栈
其实就是把前面的元素弹出,然后再到队尾,这样队列最前面的元素就变成了我们想要弹出的那个元素。所以如果length(queue) = size。我们就把前面size - 1个元素弹出即可。
即一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
初始化
queue<int> que;
void push(int x)
void push(int x){
que.push(x);
}
int pop()
int pop(){
int size = que.size();
size--;
while (size--){
que.push(que.front());
que.pop();
}
int result = que.front();
que.pop();
return result;
}
int top()
int top(){
return que.back();
}
bool empty()
bool empty(){
return que.empty();
}
CPP代码
两个队列模拟栈
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 size = que1.size();
size--;
while (size--) { // 将que1 导入que2,但要留下最后一个元素
que2.push(que1.front());
que1.pop();
}
int result = que1.front(); // 留下的最后一个元素就是要返回的值
que1.pop();
que1 = que2; // 再将que2赋值给que1
while (!que2.empty()) { // 清空que2
que2.pop();
}
return result;
}
/** Get the top element. */
int top() {
return que1.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que1.empty();
}
};
一个队列模拟栈
class MyStack {
public:
queue<int> que;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que.size();
size--;
while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
que.push(que.front());
que.pop();
}
int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
que.pop();
return result;
}
/** Get the top element. */
int top() {
return que.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
};