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

【LeetCode 经典题解】:队列与栈的双向模拟——从原理到代码详解

在这里插入图片描述

🎁个人主页:User_芊芊君子
🎉欢迎大家点赞👍评论📝收藏⭐文章
🔍系列专栏:Java.数据结构

在这里插入图片描述
在这里插入图片描述
【前言】

数据结构中,栈(后进先出)和队列(先进先出)特性迥异。本系列聚焦LeetCode经典题:队列实现栈与栈实现队列,剖析如何突破结构限制,实现特性互模拟,助你深入理解两者本质。

文章目录:

  • 一、队列实现栈
    • 1.思路分析
    • 2.代码详解
      • 2.1 push(int x)
      • 2.2 pop()
      • 2.3 top()
  • 二、栈实现队列
    • 1.思路分析
    • 2.代码分析
    • 2.1 push(int x)
    • 2.2 pop()
    • 2.3 peek()
  • 四、完整代码
    • 1.队列实现栈
    • 2.栈实现队列
  • 三、总结

一、队列实现栈

LeetCode:队列实现栈


在这里插入图片描述

1.思路分析

两个队列实现栈,使用LinkedList来模拟队列,因为LinkedList实现了Queue接口,支持队列的标准

  • 入队:统一入到不为空的队列中;
  • 出队:将size-1个元素放到另一个队列,出剩下的那一个

在这里插入图片描述

2.代码详解

创建q1,q2两个队列,并实例化,因为LinkedList实现了Queue接口

    public Queue<Integer> q1;public Queue<Integer> q2;public MyStack() {q1 = new LinkedList<>();q2 = new LinkedList<>();}

当两个队列都为空时,则栈空

 public boolean empty() {return q1.isEmpty()&& q2.isEmpty();}

2.1 push(int x)

判断q1,q2是否为空,直接添加元素到不为空的那个队列,如果都为空,就添加到q1

public void push(int x) {if(!q1.isEmpty()) {q1.offer(x);}else if(!q2.isEmpty()) {q2.offer(x);}else{q1.offer(x);}}

2.2 pop()

如果队列都为空,就返回-1。然后分开判断,q1不为空时,将它的size-1个元素转移到q2,然后出剩下的那个元素q2不为空时,将它的size-1个元素转移到q1,然后出剩下的那个元素

public int pop() {if (empty()) {return -1;}if (!q1.isEmpty()){int size = q1.size();while (size - 1 != 0) {int val = q1.poll();q2.offer(val);size--;}return q1.poll();} else{int size = q2.size();while (size - 1 != 0) {int val = q2.poll();q1.offer(val);size--;}return q2.poll();}}

2.3 top()

跟pop()一样,需要改变的就是转移元素的个数为·size·,这样转移完成时,载体val中保留的就是最后要获取的那个元素,直接返回就行

public int top() {int val = 1;if (empty()) {return -1;}if (!q1.isEmpty()){int size = q1.size();while (size != 0) {val = q1.poll();q2.offer(val);size--;}return val;} else{int size = q2.size();while (size != 0) {val = q2.poll();q1.offer(val);size--;}return val;}}

二、栈实现队列

LeetCode:栈实现队列


在这里插入图片描述

1.思路分析

用两个双端队列Deque当作栈,LinkedList实现了Deque接口(也可以用LinkedList当作栈)

  • 入栈:所有元素先放到第一个栈中;
  • 出栈
    1.如果第二个栈为空,就将s1栈中元素放到s2中,这样就会倒过来;
    2.如果不为空,直接出s2栈顶元素
  • 两个队列都为空,则模拟的栈是空的

在这里插入图片描述

2.代码分析

先创建两个栈,然后实例化。LinkedList实现了Deque接口(也可以用LinkedList当作栈)

    public Deque<Integer> s1;public Deque<Integer> s2;public MyQueue() {s1 = new LinkedList<>();s2 = new LinkedList<>();}

当两个栈都为空时,则队列空

 public boolean empty() {return s1.isEmpty()&& s2.isEmpty();}

2.1 push(int x)

直接放到s1中

public void push(int x) {s1.push(x);}

2.2 pop()

先判断栈是否为空,然后当s2为空时,就可以将s1中所有元素,转移过来,然后出s2栈顶元素

public int pop() {if(empty()) {return -1;}if(s2.isEmpty()) {while (!s1.isEmpty()) {s2.push(s1.pop());}}return s2.pop();}

2.3 peek()

与pop() 一样,转移过来后,获取s2栈顶元素

public int peek() {if(empty()) {return -1;}if(s2.isEmpty()) {while (!s1.isEmpty()) {s2.push(s1.pop());}}return s2.peek();}

四、完整代码

1.队列实现栈

public class MyStack {public Queue<Integer> q1;public Queue<Integer> q2;public MyStack() {q1 = new LinkedList<>();q2 = new LinkedList<>();}public void push(int x) {if(!q1.isEmpty()) {q1.offer(x);}else if(!q2.isEmpty()) {q2.offer(x);}else{q1.offer(x);}}public int pop() {if (empty()) {return -1;}if (!q1.isEmpty()){int size = q1.size();while (size - 1 != 0) {int val = q1.poll();q2.offer(val);size--;}return q1.poll();} else{int size = q2.size();while (size - 1 != 0) {int val = q2.poll();q1.offer(val);size--;}return q2.poll();}}public int top() {int val = 1;if (empty()) {return -1;}if (!q1.isEmpty()){int size = q1.size();while (size != 0) {val = q1.poll();q2.offer(val);size--;}return val;} else{int size = q2.size();while (size != 0) {val = q2.poll();q1.offer(val);size--;}return val;}}public boolean empty() {return q1.isEmpty()&& q2.isEmpty();}}

2.栈实现队列

public class MyQueue {public Deque<Integer> s1;public Deque<Integer> s2;public MyQueue() {s1 = new LinkedList<>();s2 = new LinkedList<>();}public void push(int x) {s1.push(x);}public int pop() {if(empty()) {return -1;}if(s2.isEmpty()) {while (!s1.isEmpty()) {s2.push(s1.pop());}}return s2.pop();}public int peek() {if(empty()) {return -1;}if(s2.isEmpty()) {while (!s1.isEmpty()) {s2.push(s1.pop());}}return s2.peek();}public boolean empty() {return s1.isEmpty()&& s2.isEmpty();}
}

三、总结

  • “队列实现栈”与“栈实现队列”均通过双结构协作+元素转移实现特性突破:前者借两个队列的转移让“先进先出”模拟“后进先出”;后者靠两个栈的分工让“后进先出”模拟“先进先出”。
  • 核心收获是理解结构特性的灵活转化——看似对立的栈和队列,可通过逻辑设计实现功能互通。掌握这种思维,能为解决复杂算法问题拓宽思路,助力你在数据结构领域的学习与实践。

在这里插入图片描述

http://www.dtcms.com/a/572710.html

相关文章:

  • 学习笔记5
  • 多线程 忙等待和线程等待的区别
  • 网站建设运维合同汽车城网站建设方案
  • 建立网站需要什么设备网站制作对公司的作用
  • C++类与对象:从入门到精通
  • UniApp 全局通知功能实现
  • uni-app开发安卓app时控制屏幕常亮不息屏
  • uniapp 小程序引入 uview plus 框架,获得精美的UI框架
  • 在被窝里使用笔记本电脑,容易损坏键盘?
  • Unix Domain Socket:构建高效本地进程间通信的完整指南
  • 如何创建“国学助手”GPT?
  • AWS Elastic Beanstalk中安装tesseract5.3.4版本
  • 实战:用Elasticsearch构建爬虫数据搜索引擎
  • 微网站建设及微信公众号长春自助建站软件
  • 修改图片网站卖房app十大排行榜
  • python-爬虫之beautifulsoup
  • Ubuntu 24.04 安装 FreeSWITCH 完整教程
  • LeetCode(python)——49.字母异位词分组
  • Redis 性能优化与故障排查指南
  • 24.java openCV4.x 入门-Imgproc之轮廓凸包与凹陷检测(形状识别)
  • IDEA 插件推荐
  • 虚拟 DOM(Virtual DOM)的工作原理及其性能优化机制
  • git详细使用教程
  • 北京工程工程建设交易信息网站和城乡建设部网站
  • soular零基础学习,如何通过工作台聚合TikLab所有工具链
  • 建立企业网站电商网站建设开题报告
  • css font-size 的妙用
  • Jenkins安装部署
  • 阿里云 CDN + 静态资源(图片 / JS/CSS)缓存优化
  • 荣耀前端开发面试题及参考答案