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

做好评做销量的网站广州google推广

做好评做销量的网站,广州google推广,网站不同时期的优化工作该怎么做,做贵网站多少钱232. 用栈实现队列 232.用栈实现队列 这里用两个栈来模拟队列以此实现队列的先进先出, 注意点 1.dump()方法将stackin的元素移入到stackout的操作,是要将stackin的全部元素放到stackout中,所以用while循环判断 stackIn是否为空 2.新建栈…

232. 用栈实现队列

232.用栈实现队列

这里用两个栈来模拟队列以此实现队列的先进先出,

注意点

1.dump()方法将stackin的元素移入到stackout的操作,是要将stackin的全部元素放到stackout中,所以用while循环判断 stackIn是否为空

2.新建栈的方法

class MyQueue {Stack<Integer> stackIn;Stack<Integer> stackOut;public MyQueue() {stackIn=new Stack<>();stackOut=new Stack<>();}public void push(int x) {// 将元素 x 推到队列的末尾stackIn.push(x);}public int pop() {//从队列的开头移除并返回元素dump();return stackOut.pop();}public int peek() {//返回队列开头的元素dump();return stackOut.peek();}public boolean empty() {// 如果队列为空,返回 true ;否则,返回 falsereturn stackIn.isEmpty()&&stackOut.isEmpty();}public void dump(){//如果stackout为空,那么把stackin的元素放到stackout中if(!stackOut.isEmpty()){return;}while(!stackIn.isEmpty()){//注意这里是while,这里如果是while(stackOut.isEmpty()),那么push进去一个就结束了// System.out.println(stackIn.pop());这里先pop了,导致后面empty()方法输出错误stackOut.push(stackIn.pop());}}
}/*** 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();* boolean param_4 = obj.empty();*/

225. 用队列实现栈

225.用队列实现栈

队列实现栈(需要二刷)

解法1 使用两个队列实现栈

  • offer() 是往队列中添加一个元素,若队列已满而仍往队列中添加,则会返回false
  • poll() 是删除队列中的第一个元素,在对空队列进行操作时,返回null
  • peek() 是输出队列的第一个元素,队列为空时,返回null
class MyStack {//使用两个队列实现站Queue<Integer> q1;Queue<Integer> q2;public MyStack() {q1=new LinkedList<>();q2=new LinkedList<>();}public void push(int x) {q2.offer(x);//先给q2添加一个元素while(!q1.isEmpty()){q2.offer(q1.poll());  //从q1拿出元素放到q2中}Queue<Integer> queueTemp;queueTemp=q1;q1=q2;q2=queueTemp;//交换q1和q2,将元素放到q1中}public int pop() {return q1.poll();}public int top() {return q1.peek();}public boolean empty() {return q1.isEmpty();}
}/*** 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();* boolean param_4 = obj.empty();*/

解法2 使用1个队列实现栈

思路是每次向队列添加一个元素后,对队列中的元素进行重新排序


class MyStack {//使用单个队列实现站Queue<Integer> q1;public MyStack() {q1=new LinkedList<>();}//每次push进来一个数,都对队列里面的数字进行重新排列public void push(int x) {q1.offer(x);//添加一个数字int size=q1.size();while(size-->1){q1.offer(q1.poll());}}public int pop() {return q1.poll();}public int top() {return q1.peek();}public boolean empty() {return q1.isEmpty();}
}/*** 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();* boolean param_4 = obj.empty();*/

http://www.tj-hxxt.cn/news/1484.html

相关文章:

  • 重庆做网站需要多少钱可以免费领取会员的软件
  • 免费建设淘宝客网站百度广告怎么投放
  • 平板电脑可以做网站不搜索网页
  • 如何做网站可以吗360优化大师app下载
  • 做360网站快速排名软件关键词搜索次数查询
  • 网站建站 优化百度客服人工电话24小时
  • html5网站欣赏网站关键词优化报价
  • 做网站要用什么服务器吗阿里域名购买网站
  • 做网站多长时间下载百度到桌面
  • 门户网站建设和检务公开自查建站 seo课程
  • 低代码网站开发平台网络营销和传统营销的区别和联系
  • 做网站需要学哪些软件会计培训班
  • 做汽车网站怎么挣钱seo排名优化厂家
  • 十大企业网站排行榜seo整站优化外包公司
  • 泰安网站建设公司带杭州seo教程
  • 搜索引擎广告形式有哪些seo快照推广
  • 口碑好的秦皇岛网站建设哪里有站长工具介绍
  • 网站建设 中山google引擎免费入口
  • logo设计公司怎么去跟客户谈现在百度怎么优化排名
  • 安徽专业做网站的大公司交易链接大全
  • 宁波手机网站开发seo到底是什么
  • 株洲网站排名优化价格百度登录入口百度
  • 深圳网站上线方案媒体:北京不再公布疫情数据
  • 使用pycharm网站开发超级外链
  • 教手工做衣服的网站网站如何做seo排名
  • 证券网站怎么做凡科建站怎么导出网页
  • 如何快速模仿一个网站南昌seo排名收费
  • 单县住房和城乡建设局网站seo智能优化软件
  • 传奇网站传奇免费网上销售平台
  • 猎头公司电话软件网站关键词优化