LeetCode算法题-Implement Queue Using Stacks(Java实现)

Raspberry Pi 4 ARM Trusted Firmware 技术解析 摘要: 本文详细介绍了在树莓派4(Raspberry Pi 4)上移植ARM可信固件(TF-A)的实现方案与安全考量。该移植专注于BL31功能,作为非安全世界(如Linux内核)的启动桥梁,支持加载EL2级别程序。文章分析了启动流程、内存映射设计,并对比了RPi3与RPi4的关键差异,指出当前版本缺乏硬件级安全内存保护的局限性。此外,提供了构建指南、配置说明及设备树自动修补的实现细节,强调通过config.txt简单配置即可启动64位内核。该方案充分利用树莓派原有GPU固件流程,兼顾兼容性与简易性。 阅读详情

这是悦乐书的第195次更新,第201篇原创

01 看题和准备

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

push(x) - 将元素x推送到队列的后面。

pop() - 从队列前面删除元素。

peek() - 获取前面的元素。

empty() - 返回队列是否为空。

例如:

MyQueue queue = new MyQueue();

queue.push(1);

queue.push(2);

queue.peek(); //返回1

queue.pop(); //返回1

queue.empty(); //返回false

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

02 第一种解法

队列的特性是先进先出,而栈的特性是先进后去,在使用栈进行队列的出队列和队顶操作时,需要借助另外一个栈来进行反转然后再还原,而入队列的操作还是无需特殊处理。

class MyQueue {

    private Stack<Integer> stack;

    /** Initialize your data structure here. */
    public MyQueue() {
        stack = new Stack<Integer>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        Stack<Integer> temp = new Stack<Integer>();
        while (!stack.isEmpty()) {
            temp.push(stack.pop());
        }
        int tem = temp.pop();
        while (!temp.isEmpty()) {
            stack.push(temp.pop());
        }
        return tem;
    }

    /** Get the front element. */
    public int peek() {
        Stack<Integer> temp = new Stack<Integer>();
        while (!stack.isEmpty()) {
            temp.push(stack.pop());
        }
        int tem = temp.peek();
        while (!temp.isEmpty()) {
            stack.push(temp.pop());
        }
        return tem;
    }

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

03 第二种解法

此解法和上面的第一种解法正好相反,是在入队列的时候,借助另外一个栈来进行反转操作,而出队列和获取队列顶的操作可以直接使用栈的方法,无需特殊处理。

class MyQueue2 {
    Stack<Integer> stack;

    /** Initialize your data structure here. */
    public MyQueue2() {
        stack = new Stack<Integer>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        Stack<Integer> temp=new Stack<>();
        while(!stack.isEmpty()){
            temp.push(stack.pop());
        }
        temp.push(x);
        while(!temp.isEmpty()){
            stack.push(temp.pop());
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack.pop();
    }

    /** Get the front element. */
    public int peek() {
        int a = stack.pop();
        stack.push(a);
        return a;
    }

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

04 第三种解法

此解法使用两个栈s1、s2来实现队列的相关操作。

入队列时,如果s2不为空,那么先把s2中的元素pop出来在push进s1,然后才去将当前要插入的数据push进s1。

出队列时,如果s2为空,即此前没有进行出队列操作或者获取队列顶的操作,那么就需要将s1反转,即将s1的元素pop出来,然后push进s2中,此时再返回s2的pop操作即可。如果s2不为空,即说明上一次操作不是入队列,而是出队列或获取队列顶的操作,直接返回s2的pop操作即可。

获取队列顶时,和入队列操作时的判断一致,只不过最后返回s2的peek操作即可。

class MyQueue3 {

    private Stack<Integer> s1;
    private Stack<Integer> s2;

    /** Initialize your data structure here. */
    public MyQueue3() {
        s1 = new Stack<Integer>();
        s2 = new Stack<Integer>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        while(!s2.empty()) {
            s1.push(s2.pop());
        }
        s1.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        } 
        return s2.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return (s1.empty() && s2.empty());
    }
}

05 第四种解法

在入队列时,始终往第一位插入元素,而其他的出队列、获取队列的顶、判空这些操作都可以直接使用栈的方法,而无需重新实现。

class MyQueue4 {

    private Stack<Integer> stack;

    /** Initialize your data structure here. */
    public MyQueue4() {
        stack = new Stack<Integer>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack.add(0, x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack.pop();
    }

    /** Get the front element. */
    public int peek() {
        return stack.peek();
    }

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

06 小结

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

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

虚拟网络的“隐形盾”:C#如何用代码把DDoS踢出门? 本文分享了如何用C#防御DDoS攻击的实战经验。通过三种核心防御策略:1)基于IP的速率限制中间件,使用滑动时间窗口算法控制请求流量;2)自动IP封禁服务,通过特征检测识别恶意IP并加入黑名单;3)优化数据库连接池和HTTP客户端连接池,合理配置资源参数。文中提供了详细的C#代码实现,包括限流中间件、IP封禁服务和连接池优化方案,并分享了生产环境中的调优经验,如配合Redis实现分布式限流、使用fail2ban自动封禁等技巧,帮助开发者构建更健壮的防御体系。 阅读详情

相关推荐

字节跳动程序媛教你如何刷算法题:面试手撕代码我就没怕过

本人上一次刷题还是大一的C语言期末考试……但是面试中算法考核截止上岸前一直保持着100%正确率的光辉记录。 本文将给你指明一条刷题的决胜指南。并从算法的重要性、题库的选择以及刷题的三大步骤分别进行讲解,彻底解决你所有关于面试算法所有的困扰。

Stay Hungry Stay Foolish 求知若饥 虚心若愚 2万+

LeetCode 232. Implement Queue using Stacks (Java)

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queu

itorly的博客 270

利用抖音Cookie充值接口提取支付链接,调起原生微信h5支付宝h5支付

最近开始搞一些个人支付通道的开发,方便个人不用和第三方平台签约就能收款,省去很多流程手续的成本。 然后翻了一下网上并没有太多现成的技术教程,只能自己研究着搞了。 这次要分享的是利用抖音的充值接口,去分析提取出支付链接,分别包含微信原生支付的h5支付链接,和支付宝h5支付链接。 微信h5支付: 抖币充值,抖音充值,抖音直播充值官方入口 - 抖音 支付宝h5支付 抖币充值,抖音充值,抖音直播充值官方入口 - 抖音 整个开发流程思路是以下几步: 1.抓包分析抖音的充值的整个流程,提取关键的一些

Android-逆向的博客 6571

Implement Stack Using LinkedList

import java.io.*; import java.util.*; /* * To execute Java, please define "static void main" on a class * named Solution. * * If you need more classes, simply define them inline. */ interface S

jialuyyy的博客 514

Power Automate Desktop实战:一键自动登录Chrome网站

本文详细介绍了如何使用Power Automate Desktop实现Chrome网站的一键自动登录,提升工作效率。从环境准备、基础配置到高级技巧与异常处理,全面解析自动化登录流程的构建与优化,特别适合需要频繁登录多个系统的用户。

weixin_29231725的博客 469

【论文分享】【Agent】ProAgent: Building Proactive Cooperative Agents with Large Language Models

多智能体系统是由多个智能体组成的集合,这些智能体可以是软件程序、机器人、传感器或其他实体,它们各自具备一定的智能和自主性,并通过交互协作来实现单个智能体难以完成的复杂目标。

nanxun___的博客 1126

java中队列算法题目_LeetCode算法题-Implement Queue Using Stacks(Java实现)

这是悦乐书的第195次更新,第201篇原创01 看题和准备今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232)。使用栈实现队列的以下操作。push(x) - 将元素x推送到队列的后面。pop() - 从队列前面删除元素。peek() - 获取前面的元素。empty() - 返回队列是否为空。例如:MyQueue queue = new MyQueue();queue.pu...

weixin_42399078的博客 257

Diebold-Mariano检验

给定两个预测的预测结果,我们希望比较他们的预测结果,以用于预测模型精度的比较。 Diebold-Mariano检验本质是一个t检验,用于检验替代预测的两个损失序列的平均值是否相等。即,它是一系列损失差的零均值的t检验。在存在自相关的情况下,它使用损失差分时间序列标准差的自相关一致性估计。 注意,在使用DM检验式时,假设损失序列是平稳的。 另外,DM检验在小样本数据时往往会拒绝零假设。对于小样本数据,推荐Harvey, Leybourne and Newbold (HLN)检验【1】; 参考文献 【1】htt

weixin_43948357的博客 6567

LeetCode232 Implement Queue using Stacks Java 题解

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front eleme

u012249528的专栏 3088

用Spirent Test Center测试交换机Head of Line(HOL)Blocking

测试目的:设备执行拥塞控制,使非阻塞端口不受阻塞端口影响 预设条件:在RFC2889里按以下步骤设置,端口1和端口3同时向端口4发送数据包,端口1向端口2发送数据包 测试步骤:运行设置好的RFC2889 测试结果:所有frame size都显示没有Head of Line(HOL),即非阻塞端口不受阻塞端口所影响 ...

Yuna_的博客 1360

python-调用动态链接库dll/so的方法

使用python ctyeps CDLL来调用linux下的so动态链接库或windows下的dll动态链接库

XIAOXIANG233233的博客 3663

leetcode Implement Queue using Stacks

继续刷题。编译CM12.1刷机怎么着都有问题,烦人的把之前下载的源码直接删掉,重新下载,在下载源码期间继续刷题,希望这个easy不要把我虐成狗。 其实主要问题是自己米有java基础知识,幸亏会点C,可以根据C又得东西查java,但这样的一个弊端时,java把C很多东西封装成了自己的类,很多东西我并不知道有那个东西的存在。。  不废话了,看题; 一.  题目   Implemen

你说你逗不逗 *^_^* 的博客 679

SAP 错误消息调试之七种武器:让所有的错误消息都能被定位

目录 长生剑 - SAPGUI Where Used List 碧玉刀 - ABAP调试器观察点 霸王枪 - ABAP调试器动态断点 多情环 - ABAP代码静态扫描 孔雀翎 - SAT 离别钩 - ST05 拳头 - 迷之方法??? Jerry在2018年开始用中文在微信公众号平台上写作之前,在SAP官方社区上总共写了639篇英文博客。 其中有一篇写于2013年介绍ABAP调试技巧的博客:

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 2813

【Leetcode】Implement Queue using Stacks

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes

业精于勤,荒于嬉 2243

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leetcode_232_Implement Queue using Stacks

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

皮斯特劳沃 2253

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

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

LearnHowToFly的博客 603

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode-232. Implement Queue using Stacks [C++][Java]

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push,peek,pop, andempty).

贫道绝缘子的博客 483

【leetcode】【232】Implement Queue using Stacks

一、问题描述 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front el

斗code、年华 349

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

翻译用栈来实现队列的下列操作。push(x) —— 将元素x写入到队列的尾部 pop() —— 从队列首部移除元素 peek() —— 返回队列首部元素 empty() —— 返回队列是否为空注意:你必须使用一个只有标准操作的栈。也就是说,只有push/pop/size/empty等操作是有效的。栈可能不被原生支持,这取决于你所用的语言。只要你只是用stack的标准操作,你可以用list或者dequ

nomasp 3848

Implement Queue using Stacks

解题思路:采用两个栈,实现队列,一个用于进栈S1,一个用于出栈和取头元素S2。进栈时必须将S2中元素全部加入S1中,出栈时必须将S1中的元素加入S2中,才可以保证先进先出。判断为空时,S1,S2均为空。 Java代码实现: class MyQueue { private Stack stack1; private Stack stack2; MyQueue(){ stack1=n

宋烈金的博客 435

LeetCode 232. Implement Queue using Stacks

欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-232-implement-queue-using-stacks/ 解答[Java] import java.util.ArrayDeque; import java.util.Deque; class MyQueue { Deque<Integer> stack1; ...

user2639的博客 180

leetcode oj java Implement Queue using Stacks

一、问题描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front e

Xcy的博客 278

leetcode之Implement Queue using Stacks

C++标准里面对于栈,总共就给出了四种操作:入栈push,出栈pop,检查是否为空empty,返回栈的顶top。本题考查用栈的四种操作组合来实现队列queue的操作,可谓考查数据结构的基本功。解决方法之一是,使用两个栈,一个负责“进”,另一个负责“出”。难点是队列的出队操作,也就是pop,当负责“”的栈是空的时候同时负责进的栈非空,则此时先将负责“进”的栈的所有元素依次全部pop出来再依次全部pu...

代码草根的专栏 276

[Leetcode 232, Easy] Implement Queue using Stacks

[Leetcode 232, Easy] Implement Queue using Stacks

算法的实践 435

Java [Leetcode 232]Implement Queue using Stacks

题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the fron...

dkk37351的博客 63

Leetcode: 232. Implement Queue using Stacks(JAVA)

【问题描述】 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front el

莫名的程序猿 717
上一篇: LeetCode算法题-Power Of Two(Java实现)
下一篇: LeetCode算法题-Palindrome Linked List(Java实现)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值