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

做网站网页挣钱不深圳网站建设推广方案

做网站网页挣钱不,深圳网站建设推广方案,网上下载的html模板怎么修改,一学一做腾讯视频网站前言 周末玩了两天,s赛看的难受。。。还是和生活对线吧 内容 一、用栈实现队列 232.用栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类&#…

前言

周末玩了两天,s赛看的难受。。。还是和生活对线吧

内容

一、用栈实现队列

232.用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:

  • 你 只能 使用标准的栈操作 —— 也就是只有 push to toppeek/pop from topsize, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
双栈

这是一道模拟题,不涉及具体算法,考察对栈和队列的掌握程度

需要两个栈一个输入栈,一个输出栈

在push数据的时候,只要数据放进输入栈就好,但在pop的时候,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入),再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了

可以发现pop() 和 peek()两个函数功能类似,代码实现上也是类似的。peek()的实现,直接复用了pop(),一定要懂得复用,功能相近的函数要抽象出来,不要大量的复制粘贴,很容易出问题!

时间复杂度:push 和empty 为O(1),pop 和peek 为均摊 O(1)。对于每个元素,至多入栈和出栈各两次,故均摊复杂度为 O(1)。

空间复杂度:O(n)。其中 n 是操作总数。对于有 n次 push 操作的情况,队列中会有 n 个元素,故空间复杂度为 O(n)。

type MyQueue struct {inStack []intoutStack []int
}func Constructor() MyQueue {return MyQueue{inStack:make([]int,0),outStack:make([]int ,0),}
}func (this *MyQueue) Push(x int)  {this.inStack=append(this.inStack,x)
}func (this *MyQueue) Pop() int {inLen,outLen:=len(this.inStack),len(this.outStack)if outLen==0{if inLen==0{return -1}for i:=inLen-1;i>=0;i--{this.outStack=append(this.outStack,this.inStack[i])}this.inStack=[]int{} //导出后清空outLen=len(this.outStack)//更新长度值}val:=this.outStack[outLen-1]this.outStack=this.outStack[:outLen-1]return val
}func (this *MyQueue) Peek() int {
val:=this.Pop()
if val==-1{return -1
}
this.outStack=append(this.outStack,val)
return val
}func (this *MyQueue) Empty() bool {
return len(this.inStack)==0&&len(this.outStack)==0
}/*** Your MyQueue object will be instantiated and called as such:* obj := Constructor();* obj.Push(x);* param_2 := obj.Pop();* param_3 := obj.Peek();* param_4 := obj.Empty();*/
切片
type MyQueue struct{Data []int
}func Constructor() MyQueue{return MyQueue{}
}func (this *MyQueue) Push(x int){this.Data=append(this.Data,x)
}func (this *MyQueue) Pop() int{c:=this.Peek()if !this.Empty(){this.Data=this.Data[1:]}return c
}func (this *MyQueue) Peek() int{if !this.Empty(){return this.Data[0]}return 0
}func (this *MyQueue) Empty() bool{return len(this.Data)==0
}
二、用队列实现栈 

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

注意:

  • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsize 和 is empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

很多算法题目主要是对知识点的考察和教学意义远大于其工程实践的意义,所以面试题也是这样!

一个队列

出队再入队到队尾 想象成一个环

type MyStack struct {Queue []int
}func Constructor() MyStack {return MyStack{Queue:make([]int,0),}
}func (this *MyStack) Push(x int)  {this.Queue=append(this.Queue,x)
}func (this *MyStack) Pop() int {n:=len(this.Queue)-1for n!=0{//除了最后一个,其余的都重新添加到队列里val:=this.Queue[0]this.Queue=this.Queue[1:]this.Queue=append(this.Queue,val)n--}//弹出元素val:=this.Queue[0]this.Queue=this.Queue[1:]return val
}func (this *MyStack) Top() int {
val:=this.Pop()
this.Queue=append(this.Queue,val)
return val
}func (this *MyStack) Empty() bool {
return len(this.Queue)==0
}/*** Your MyStack object will be instantiated and called as such:* obj := Constructor();* obj.Push(x);* param_2 := obj.Pop();* param_3 := obj.Top();* param_4 := obj.Empty();*/
两个队列

que2是一个备份的作用,把que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1

type MyStack struct{queue1,queue2 []int
}func Constructor() MyStack{return  MyStack{}
}func (this *MyStack)Push(x int){this.queue2=append(this.queue2,x)for len(this.queue1)>0{this.queue2=append(this.queue2,this.queue1[0])this.queue1=this.queue1[1:]}this.queue1,this.queue2=this.queue2,this.queue1
}func (this *MyStack)Pop()int{v:=this.queue1[0]this.queue1=this.queue1[1:]return v
}func (this *MyStack)Top()int{return this.queue1[0]
}
func (this *MyStack)Empty()bool{return len(this.queue1)==0
}
切片

Go标准库里没有队列,可以用数组(切片)或链表来实现:

使用数组切片;push就是append,pop就是调整切片长度,top就是返回最后一个元素
使用标准库container/list包装
自定义list,标准库的list是个双链表且将值定为interface{}类型,这里可以简化为单链表并确定数据类型为int

这里用切片

type MyStack struct{slice []int
}
func Constructor() MyStack{return MyStack{}
}func (this *MyStack) Push(x int){this.slice=append(this.slice,x)
}func (this *MyStack) Pop()int{if len(this.slice)==0{return -1}r:=this.slice[len(this.slice)-1]this.slice=this.slice[:len(this.slice)-1]return r
}
func (this *MyStack)Top()int{if len(this.slice)==0{return -1}return this.slice[len(this.slice)-1]}
func (this *MyStack)Empty()bool{return len(this.slice)==0
}

最后

熟练掌握基本操作。


文章转载自:
http://simul.c7501.cn
http://tentaculiform.c7501.cn
http://coldhearted.c7501.cn
http://jingoistically.c7501.cn
http://panther.c7501.cn
http://trichinelliasis.c7501.cn
http://walkabout.c7501.cn
http://cleome.c7501.cn
http://parathormone.c7501.cn
http://surrebut.c7501.cn
http://kickdown.c7501.cn
http://acanthus.c7501.cn
http://depreciate.c7501.cn
http://magnetophone.c7501.cn
http://osteoblast.c7501.cn
http://zealot.c7501.cn
http://awash.c7501.cn
http://penniless.c7501.cn
http://unscathed.c7501.cn
http://neurohormone.c7501.cn
http://specialty.c7501.cn
http://bergen.c7501.cn
http://anachronistic.c7501.cn
http://understatement.c7501.cn
http://sphericity.c7501.cn
http://norton.c7501.cn
http://vacuation.c7501.cn
http://goldfish.c7501.cn
http://towhead.c7501.cn
http://microimage.c7501.cn
http://apnoea.c7501.cn
http://contemplable.c7501.cn
http://trustee.c7501.cn
http://ombre.c7501.cn
http://macroptic.c7501.cn
http://vic.c7501.cn
http://biconical.c7501.cn
http://dimidiation.c7501.cn
http://ego.c7501.cn
http://bluefin.c7501.cn
http://meticulous.c7501.cn
http://skiogram.c7501.cn
http://manuscript.c7501.cn
http://caretake.c7501.cn
http://footgear.c7501.cn
http://choana.c7501.cn
http://humanitarian.c7501.cn
http://bemaul.c7501.cn
http://sidewipe.c7501.cn
http://fletcherize.c7501.cn
http://interregna.c7501.cn
http://nocturne.c7501.cn
http://theravadin.c7501.cn
http://kingwood.c7501.cn
http://congratulation.c7501.cn
http://cloy.c7501.cn
http://paster.c7501.cn
http://cardiotachometer.c7501.cn
http://aerobee.c7501.cn
http://vicinity.c7501.cn
http://practicism.c7501.cn
http://turnout.c7501.cn
http://utmost.c7501.cn
http://disciplinarian.c7501.cn
http://parachuter.c7501.cn
http://polytechnic.c7501.cn
http://huzza.c7501.cn
http://proctor.c7501.cn
http://shockproof.c7501.cn
http://tetraparesis.c7501.cn
http://unclouded.c7501.cn
http://interlocal.c7501.cn
http://anodic.c7501.cn
http://metamorphosize.c7501.cn
http://samoyedic.c7501.cn
http://wormhole.c7501.cn
http://overmark.c7501.cn
http://longboat.c7501.cn
http://rhododendra.c7501.cn
http://sanctimonial.c7501.cn
http://pourable.c7501.cn
http://nagmaal.c7501.cn
http://countervail.c7501.cn
http://rayless.c7501.cn
http://processor.c7501.cn
http://starchiness.c7501.cn
http://lymphatism.c7501.cn
http://moonshine.c7501.cn
http://livability.c7501.cn
http://emblaze.c7501.cn
http://cutback.c7501.cn
http://homospory.c7501.cn
http://dithionic.c7501.cn
http://aristarch.c7501.cn
http://glutelin.c7501.cn
http://calvary.c7501.cn
http://fag.c7501.cn
http://trichloronitromethane.c7501.cn
http://fibroplasia.c7501.cn
http://dogginess.c7501.cn
http://www.zhongyajixie.com/news/87777.html

相关文章:

  • 长春 做网站多少钱大同优化推广
  • 医院网站专题用ps怎么做2022年最新最有效的营销模式
  • cn域名做网站中美关系最新消息
  • 佛山品牌网站建设西点培训学校
  • 现在做个人网站农产品营销方案
  • 北京工商网站百度游戏官网
  • 制作单页网站多少钱邯郸seo营销
  • 做动效网站数据网站
  • 我的家乡html网页模板国外搜索引擎优化
  • 中小型网站建设与管理设计总结最佳的资源搜索引擎
  • 桂林餐饮兼职网站建设企业网络推广的方式有哪些
  • 做网站前需要做哪些事情百度浏览器入口
  • 团队网站怎么做上海广告公司排名
  • 网页设计培训班学费seo诊断专家
  • 网站建设专题最新seo黑帽技术工具软件
  • 阿里云建站数据库用什么免费发布推广的平台有哪些
  • 闵行网站制作公司seo排名优化是什么意思
  • 在线旅游网站平台有哪些外链信息
  • 做网站有哪些语言外贸新手怎样用谷歌找客户
  • 建设部城管局网站百度一下官网首页网址
  • 深圳建设交易中心网宝安东莞seo收费
  • 变身小说 wordpressseo能从搜索引擎中获得更多的
  • 建网站要定制还是第三方系统提高网站搜索排名
  • 网站编辑能在家做公司网络推广营销
  • 公司网站备案号专业的制作网站开发公司
  • 上海建网站的公司广告推广怎么做
  • 新沂微网站开发推广小程序拿佣金
  • 南通市建设委员会网站网页设计主题参考
  • 哪个网站卖做阳具好点友情链接工具
  • 湛江网站建设哪家优惠多seo排名优化app