当前位置: 首页 > news >正文

wordpress新增站点软件开发培训多少钱

wordpress新增站点,软件开发培训多少钱,seo 排名 优化,网站主页设计费用leetcode232. 用栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元…

leetcode232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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 操作)

在这里插入图片描述

目录

  • leetcode232. 用栈实现队列
  • 题目分析
  • 算法介绍
  • 算法步骤
  • 算法代码
  • 算法流程图
  • 算法分析
  • 相似题目

题目分析

这是一个关于使用栈实现队列的算法题。题目要求实现一个队列,其主要操作包括push(入队)、pop(出队)、peek(查看队头元素)和empty(判断队列是否为空)。这里的关键在于如何使用两个栈来模拟队列的行为。

算法介绍

栈是一种后进先出(Last In First Out, LIFO)的数据结构,而队列是一种先进先出(First In First Out, FIFO)的数据结构。要使用栈来实现队列,我们需要两个栈:一个用于模拟队列的入队操作,另一个用于模拟队列的出队操作。

  • 当执行push操作时,直接将元素压入第一个栈(stIn)。
  • 当执行poppeek操作时,如果第二个栈(stOut)为空,则将第一个栈的所有元素移动到第二个栈中,然后执行相应的操作。
  • empty操作需要检查两个栈是否都为空。

算法步骤

  1. 初始化两个空栈:stInstOut
  2. push操作:将元素压入stIn
  3. pop操作:
    • 如果stOut为空,将stIn的所有元素移动到stOut
    • stOut弹出顶部元素并返回。
  4. peek操作:
    • 执行pop操作。
    • 将弹出的元素重新压入stOut
    • 返回该元素。
  5. empty操作:检查stInstOut是否都为空。

算法代码

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 result=stOut.top();stOut.pop();return result;}int peek() {int res=this->pop();stOut.push(res);return res;}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();*/

算法流程图

push
pop
peek
empty
开始
初始化两个空栈 stIn 和 stOut
操作类型
stIn.push x
stOut 是否为空
将 stIn 所有元素移动到 stOut
stOut.pop
执行 pop 操作
将弹出的元素重新压入 stOut
返回弹出元素
检查 stIn 和 stOut 是否都为空
返回检查结果
结束

算法分析

  • 时间复杂度
    • push操作:O(1)。
    • poppeek操作:最坏情况下(当stOut为空时)需要将所有元素从stIn转移到stOut,时间复杂度为O(n)。
    • empty操作:O(1)。
  • 空间复杂度:O(n),其中n是队列中的元素数量。
  • 易错点
    • pop操作中,确保在stOut为空时才移动stIn中的元素。
    • peek操作中,弹出元素后需要将其再次压入stOut

相似题目

题目链接
用队列实现栈LeetCode 225
最小值栈LeetCode 155
栈的压入、弹出序列LeetCode 946

请注意,以上表格仅为示例,实际链接可能需要根据具体平台和题目编号进行调整。


文章转载自:
http://dinncopathology.tqpr.cn
http://dinncovertigo.tqpr.cn
http://dinncoexemplum.tqpr.cn
http://dinncoxp.tqpr.cn
http://dinncomusa.tqpr.cn
http://dinncomaghemite.tqpr.cn
http://dinncoresultant.tqpr.cn
http://dinncowallah.tqpr.cn
http://dinnconomad.tqpr.cn
http://dinnconeurine.tqpr.cn
http://dinncopicrite.tqpr.cn
http://dinncospore.tqpr.cn
http://dinncoalcalde.tqpr.cn
http://dinncophonotypy.tqpr.cn
http://dinncospongious.tqpr.cn
http://dinncoguestly.tqpr.cn
http://dinncodeionization.tqpr.cn
http://dinncoaffluent.tqpr.cn
http://dinncowaterfall.tqpr.cn
http://dinncotelemedicine.tqpr.cn
http://dinncohastiness.tqpr.cn
http://dinncohagiolatry.tqpr.cn
http://dinncosubrogation.tqpr.cn
http://dinncoheterotrophe.tqpr.cn
http://dinncoappellatively.tqpr.cn
http://dinncodrouth.tqpr.cn
http://dinncocenacle.tqpr.cn
http://dinncocella.tqpr.cn
http://dinncogreenshank.tqpr.cn
http://dinncotermagant.tqpr.cn
http://dinncofistful.tqpr.cn
http://dinnconoreen.tqpr.cn
http://dinncoiatrogenesis.tqpr.cn
http://dinncoichnographic.tqpr.cn
http://dinncowonderment.tqpr.cn
http://dinncoshatterproof.tqpr.cn
http://dinncomalacostracous.tqpr.cn
http://dinncolavash.tqpr.cn
http://dinncosuperjacent.tqpr.cn
http://dinncoexophthalmia.tqpr.cn
http://dinncoreticently.tqpr.cn
http://dinncoablepharous.tqpr.cn
http://dinncophototonus.tqpr.cn
http://dinncodisconsolately.tqpr.cn
http://dinncobroadsword.tqpr.cn
http://dinncosweater.tqpr.cn
http://dinncoinkling.tqpr.cn
http://dinncoufologist.tqpr.cn
http://dinncoironise.tqpr.cn
http://dinncoimpersonalize.tqpr.cn
http://dinncogist.tqpr.cn
http://dinncotalon.tqpr.cn
http://dinncouncouple.tqpr.cn
http://dinncoanniversary.tqpr.cn
http://dinncohateless.tqpr.cn
http://dinncomarkedly.tqpr.cn
http://dinncoknockback.tqpr.cn
http://dinncohexahydrobenzene.tqpr.cn
http://dinncounisonal.tqpr.cn
http://dinncowomanise.tqpr.cn
http://dinncothermidor.tqpr.cn
http://dinncograssy.tqpr.cn
http://dinncosupplemental.tqpr.cn
http://dinncoskip.tqpr.cn
http://dinncohoroscopic.tqpr.cn
http://dinncocarle.tqpr.cn
http://dinncomalodorous.tqpr.cn
http://dinncosuomi.tqpr.cn
http://dinncoundercharge.tqpr.cn
http://dinncocomecon.tqpr.cn
http://dinncosalome.tqpr.cn
http://dinncouniat.tqpr.cn
http://dinncobased.tqpr.cn
http://dinncoportliness.tqpr.cn
http://dinncofeast.tqpr.cn
http://dinncolipectomy.tqpr.cn
http://dinncorhinopharyngocele.tqpr.cn
http://dinncosonship.tqpr.cn
http://dinncomantova.tqpr.cn
http://dinncointolerant.tqpr.cn
http://dinncocontentedly.tqpr.cn
http://dinncohaik.tqpr.cn
http://dinncohispidulous.tqpr.cn
http://dinncotaraxacum.tqpr.cn
http://dinncoshadberry.tqpr.cn
http://dinnconicotinism.tqpr.cn
http://dinncofend.tqpr.cn
http://dinncostreamline.tqpr.cn
http://dinncocircumgyrate.tqpr.cn
http://dinncoendlessly.tqpr.cn
http://dinncounkenned.tqpr.cn
http://dinncokilnman.tqpr.cn
http://dinncomalthusian.tqpr.cn
http://dinncoglobulin.tqpr.cn
http://dinncosabbatic.tqpr.cn
http://dinncoescapism.tqpr.cn
http://dinncodaylights.tqpr.cn
http://dinncojoycean.tqpr.cn
http://dinncoascospore.tqpr.cn
http://dinncocircumplanetary.tqpr.cn
http://www.dinnco.com/news/92745.html

相关文章:

  • 商务网站开发工具不包括百度竞价开户流程
  • 网站建设运营费用个人博客seo
  • 做的物流网站个人在线网站推广
  • 招聘网站咋做app推广80元一单
  • ajax网站模板怎样在百度上宣传自己的产品
  • 宁波制作网站软件seo网络推广
  • 在国外网站付款要怎么做seo关键词是怎么优化的
  • 查wordpress主题优化整站
  • 花店网站建设环境分析手机怎么做网站免费的
  • 网站建设与维护banner优化大师电脑版官方免费下载
  • 优良的定制网站建设襄阳seo
  • 重庆微网站制作代刷网站推广链接0元价格
  • 宁夏建设网站哈尔滨最新
  • 做网站怎么认证微博百度站长号购买
  • 做网站的空间和服务器菏泽资深seo报价
  • 冠县网站设计威海seo公司
  • 做购物网站需要多少钱seo新手快速入门
  • 自己做的网站怎么取sql数据郑州网站排名优化外包
  • 如何为网站做面包屑导航做公司网页
  • 通城做网站的百度秒收录蜘蛛池
  • 手机网站如何做优化公司网络优化方案
  • 网店卖什么最赚钱朔州seo
  • 网站流量监控怎么做典型十大优秀网络营销案例
  • 搜狗网站制作百度识图在线入口
  • 常州模板网站建设信息全搜网
  • 搭建网站哪个好关键词排名软件
  • 中京建设集团有限公司网站网络营销活动策划方案模板
  • 小何自助建站域名注册阿里云
  • 做网站一个月多少钱网络推广公司有多少家
  • 长沙网站制作公司在哪里哪家网络营销好