LeetCode算法题-Implement Stack Using Queues

别再让充电器‘罢工’了!聊聊Charger里VIN-DPM这个不起眼但关键的功能 本文深入探讨了充电IC中的VIN-DPM(输入电压动态电源管理)功能,揭示其如何智能调节充电策略以避免电源适配器过载。通过实际案例和实验室数据,展示了VIN-DPM在提升充电稳定性和效率中的关键作用,并提供了选型、设计和故障排查的实用指南。 阅读详情

这是悦乐书的第193次更新,第198篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第54题(顺位题号是225)。使用队列实现栈的以下操作:

push(x) - 将元素x推入栈。
pop() - 删除栈顶部的元素。
top() - 获取顶部元素。
empty() - 返回栈是否为空。

例如:

MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.top(); //返回2
stack.pop(); //返回2
stack.empty(); //返回false

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

借助ArrayList。进栈利用list的add方法。出栈时先获取list最后一位元素存起来,然后再删除最后一位元素,再返回先前存起来的最后一位元素。获取栈顶时,返回list最后一位元素即可。判空可以直接利用list的isEmpty方法。

class MyStack {
    List<Integer> list = null;
    /** Initialize your data structure here. */
    public MyStack() {
        list = new ArrayList<Integer>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        list.add(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        int top = list.get(list.size()-1);
        list.remove(list.size()-1);
        return top;
    }

    /** Get the top element. */
    public int top() {
        return list.get(list.size()-1);
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return list.isEmpty();
    }
}

03 第二种解法

利用队列。

进栈直接使用queue的add方法或者offer方法。

出栈时,因为队列是先进先出的特性,所以需要先把队列里的元素poll出来再add进去,但是此操作只能进行queue的size-1次,比如:进栈顺序是1->2->3,进行一次出队列再进队列操作后,2->3->1,再进行一次出队列再进队列的操作,3->1->2,如果你再操作一次就还原了,所以只能操作queue的size-1次。最后再使用队列的poll方法,实现出栈。

栈顶,和出栈的思路一样,不过在把队列里的元素poll出来再add进去size-1次后,先要将顶获取到,可以直接使用队列的peek方法,然后再来一次poll出来再add进去,就把队列元素的顺序还原了。方便下次操作。

判空可以直接使用队列自身的isEmpty方法。

class MyStack2 {

    Queue<Integer> queue = null;
    int size ;

    /** Initialize your data structure here. */
    public MyStack2() {
        queue = new LinkedList<Integer>();
        size = 0;
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
        size++;
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        for (int i=1; i<size; i++) {
            queue.add(queue.poll());
        }
        size--;
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        for (int i=1; i<size; i++) {
            queue.add(queue.poll());
        }
        int res = queue.peek();
        queue.add(queue.poll());
        return res;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

04 第三种解法

还是使用队列。不过此解法除了入栈重新实现外,其他的出栈、栈顶、判空都是使用了队列的方法,所以重点讲下入栈即可。

入栈时,首先利用队列的add方法,然后利用循环,把队列里的元素poll出来再add进去,此循环只执行队列的size-1次,执行size次会还原。

class MyStack3 {

    Queue<Integer> queue = null;

    /** Initialize your data structure here. */
    public MyStack3() {
        queue = new LinkedList<Integer>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
        for (int i=0; i<queue.size()-1; i++) {
            queue.add(queue.poll());
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        return queue.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

05 小结

算法专题目前已连续日更超过一个月,算法题文章54+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

Cesium 实战 28 - 三维视频融合(视频投影) Cesium 实战 27 - 三维视频融合(视频投影)。 阅读详情

相关推荐

六维力传感器选型指南:电阻式vs电容式,哪个更适合你的工业场景?

本文深入解析六维力传感器在工业自动化中的选型要点,重点对比电阻式与电容式传感器的核心原理、环境适应性及成本结构。针对不同工业场景(如高精度装配、重载机器人等),提供专业选型建议,帮助工程师优化系统性能与经济效益。

weixin_42525005的博客 398

【Leetcode】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the elemen

业精于勤,荒于嬉 2205

优快云余额充值前必读:这5个隐藏条款,90%的开发者都忽略了(附iOS内购避坑指南)

本文揭示了优快云余额充值中90%开发者忽略的5个隐藏条款,包括不可退款规则、iOS内购限制、账号安全责任等关键内容,特别提供了iOS内购避坑指南,帮助开发者避免资金损失和账号风险。

weixin_26765287的博客 151

LeetCode Implement Stack using Queues

Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whether

Coding for Dreams 3125

深入解析LVDS电平标准在LCD屏接口设计中的关键作用

本文深入解析了LVDS电平标准在LCD屏接口设计中的核心作用。文章不仅阐释了LVDS低电压差分信号的工作原理、关键电气参数及抗噪优势,还详细对比了VESA与JEIDA两种主流数据格式,并提供了从PCB布局布线到信号调试的实战避坑指南,是确保高速、稳定显示的关键技术参考。

weixin_28419039的博客 339

基于FPGA与红外成像的眼动追踪系统设计与实现(教学向)

本文详细介绍了基于FPGA与红外成像的眼动追踪系统设计与实现。通过红外摄像头采集图像,利用FPGA进行实时图像处理和红外瞳孔追踪,结合Verilog编程实现多目标检测与瞳孔定位。该系统适用于嵌入式视觉教学,展示了硬件搭建、算法优化及实时显示的完整流程。

g5h6i7j的博客 823

【LeetCode】Implement Stack using Queues 解题报告

【题目】 Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- R

跳出温水的青蛙 1万+

nmap、X-Scan漏洞扫描和Metasploit中用永恒之蓝漏洞入侵靶机并上传冰河

利用nmap、X-Scan扫描靶机的漏洞,在Metasploit中用扫描出的永恒之蓝漏洞入侵靶机,建立连接后上传冰河木马控制靶机,最后用冰河陷阱清除靶机的冰河木马,记一次完整的入侵控制实验。

m0_59302403的博客 4410

LeetCode 225: Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet

哈哈的个人专栏 6893

VIVADO IP核之FIR抽取器多相滤波仿真

关于FIR低通滤波器和多相滤波插值器的使用,我之前的文章已经介绍过了,本文将继续深入介绍FIR抽取器多相滤波的使用方法,并将FIR抽取多相滤波的结果与MATLAB仿真计算的结果比较,验证了FIR抽取器多相滤波使用正确。

m0_66360845的博客 3699

群辉NAS映射为本地盘符

鼠标右击网络→映射网络驱动器→以上适合通过局域网操作,采用ZeroTier组网后,也可以按照以上步骤实现,这时使用 “虚拟局域网”的IP地址。

dgnankai的专栏 5778

leetcode 225: Implement Stack using Queues

leetcode 225: Implement Stack using Queues python java c++

西施豆腐渣 5108

simulink的电动汽车制动系统热管理仿真

本文系统研究了电动汽车制动系统热管理仿真技术。通过建立机电液多物理场耦合模型,分析了不同工况下的动态热平衡特性。提出基于MPC的动态调节和NSGA-II多目标优化算法,使电池温度波动降低60%。创新性采用石墨烯相变材料与微通道液冷复合技术,冷却系统能耗减少40%。研究构建了完整的工程验证平台,涵盖200kW电制动测试台和高精度热成像系统。展望指出,未来将结合Transformer预测和强化学习算法,进一步优化热管理策略。该研究为电动汽车制动系统热管理提供了从理论建模到工程实施的完整解决方案。

xiaoheshang_123的博客 123

闯关leetcode——225. Implement Stack using Queues

这题就是要求使用两个队列模拟栈的操作。

方亮的专栏 872

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode225:Implement Stack using Queues

Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether

1573

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on

1302

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[LeetCode 225] Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet

coder 进阶的专栏 822

Leetcode_252_Implement Stack using Queues

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.youkuaiyun.com/pistolove/article/details/48598773 Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() --

皮斯特劳沃 1297

[LeetCode] Implement Stack using Queues

Implement Stack using Queues Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the ...

weixin_34067102的博客 125

Implement Stack using Queues && Implement Queue using Stacks (LeetCode)

Implement Stack using Queues 即使用队列来实现栈 方法:创建两个队列,始终保持一个队列为空。添加数据时,添加到空队列,然后将另一个队列加入到该队列中,这样就保证了最后添加的数据处于队列的队首(即满足栈的先进后出、后进先出的要求);取数据时,从非空队列使用队列的方法中取出的数据即为所求 具体代码如下: class MyStack {       //始终保持一

LearnHowToFly的博客 603

LeetCode225. Implement Stack using Queues

【代码】LeetCode225. Implement Stack using Queues。

叶卡捷琳堡的博客 882

LeetCode题解——Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet

susandebug 487

LeetCode-225. Implement Stack using Queues [C++][Java]

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push,top,pop, andempty).

贫道绝缘子的博客 334

leetcode oj java Implement Stack using Queues

一、问题描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -

Xcy的博客 293

LeetCode-Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet

My_Jobs的专栏 775

LeetCode 225. Implement Stack using Queues 题解

本题是栈和队列的经典应用问题,主要考察对栈和队列数据结构的理解和使用。通过使用两个队列,我们可以高效地实现栈的所有操作。双队列的核心思想是:使用两个队列,一个主队列用于存储元素,一个辅助队列用于临时存储元素,当需要压入元素时,将元素压入辅助队列,然后将主队列的所有元素转移到辅助队列,最后交换主队列和辅助队列。这种方法不仅适用于用队列实现栈问题,还可以应用于许多其他需要栈和队列转换的问题。掌握双队列的使用,对于解决这类问题非常重要。

cannonjinx的博客 4576
上一篇: LeetCode算法题-Contains Duplicate II(Java实现)
下一篇: LeetCode算法题-Invert Binary Tree
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值