网站打开慢什么原因台州网站建设外包
网站打开慢什么原因,台州网站建设外包,关于公司网站的建设的问卷,商丘柘城做网站目录
一、232. 用栈实现队列
二、225. 用队列实现栈 一、232. 用栈实现队列 题目链接#xff1a;力扣
文章讲解#xff1a;代码随想录
视频讲解#xff1a; 栈的基本操作#xff01; | LeetCode#xff1a;232.用栈实现队列
题目#xff1a;
请你仅使用两个栈实现先…目录
一、232. 用栈实现队列
二、225. 用队列实现栈 一、232. 用栈实现队列 题目链接力扣
文章讲解代码随想录
视频讲解 栈的基本操作 | LeetCode232.用栈实现队列
题目
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作push、pop、peek、empty
实现 MyQueue 类
void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek() 返回队列开头的元素 boolean empty() 如果队列为空返回 true 否则返回 false
代码
class MyQueue {
public:stackint stackin;stackint stackout;MyQueue() {}void push(int x) {stackin.push(x);return;}int pop() {if (stackout.empty()) while(!stackin.empty()) {stackout.push(stackin.top());stackin.pop();}int result stackout.top();stackout.pop();return result;}int peek() {int res pop();stackout.push(res);return res;}bool empty() {return stackin.empty() stackout.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();*/
总结两个栈组成一个队列栈1入栈栈2出栈颠倒两次负负得正。
二、225. 用队列实现栈
题目链接力扣
文章讲解代码随想录
视频讲解队列的基本操作 | LeetCode225. 用队列实现栈
题目请你仅使用两个队列实现一个后入先出LIFO的栈并支持普通栈的全部四种操作push、top、pop 和 empty。实现 MyStack 类
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的返回 true 否则返回 false 。代码
class MyStack {
public:queueint queue;MyStack() {}void push(int x) {queue.push(x);}int pop() {int size queue.size();while(--size){queue.push(queue.front());queue.pop();}int ans queue.front();queue.pop();return ans;}int top() {return queue.back();}bool empty() {return queue.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();*/
总结把第一队列中前面的元素添加到队列的后面留下最后一个元素输出即为栈的pop。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/90777.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!