码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 面试题 03.04. 化栈为队


    文章目录

    • 一、题目
    • 二、C# 题解

    一、题目

      实现一个MyQueue类,该类用两个栈来实现一个队列。

      点击此处跳转题目。

    示例:

    MyQueue queue = new MyQueue();
    queue.push(1);
    queue.push(2);
    queue.peek(); // 返回 1
    queue.pop(); // 返回 1
    queue.empty(); // 返回 false

    说明:

    • 你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size 和 is empty 操作是合法的。
    • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
    • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

    二、C# 题解

      很简单的题目,进队列时将元素压入 inStack 中,出队列时将 inStack 元素顺序压入 outStack 后弹出顶端元素即可。

    public class MyQueue {
        private Stack<int> inStack, outStack;
    
        /** Initialize your data structure here. */
        public MyQueue() {
            inStack = new Stack<int>();
            outStack = new Stack<int>();
        }
        
        /** Push element x to the back of queue. */
        public void Push(int x) {
            Reverse(outStack, inStack);
            inStack.Push(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        public int Pop() {
            Reverse(inStack, outStack);
            return outStack.Pop();
        }
        
        /** Get the front element. */
        public int Peek() {
            Reverse(inStack, outStack);
            return outStack.Peek();
        }
        
        /** Returns whether the queue is empty. */
        public bool Empty() {
            return (inStack.Count | outStack.Count) == 0;
        }
    
        // 将 st1 中的元素压入 st2 中
        private void Reverse(Stack<int> st1, Stack<int> st2) {
            while (st1.Count != 0) st2.Push(st1.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();
     * bool param_4 = obj.Empty();
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 时间复杂度:无。
    • 空间复杂度:无。
  • 相关阅读:
    基于ssm阳光心理健康网站的设计与实现-计算机毕业设计源码+LW文档
    牛客网:NC170 最长不含重复字符的子字符串
    C/C++鸡尾酒疗法 2023年5月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
    Oracle EBS R12 DBA(一)
    密度估计公式
    基于Java+SpringBoot+Vue前后端分离码头船只货柜管理系统设计和实现
    电脑白屏桌面循环切换,硬盘bitlocker重要数据无法导出
    第十届MathorCup高校数学建模挑战赛-B题:养老服务床位需求预测与运营模式研究(续)
    物联网与 Linux 的相爱相生
    联邦学习-Tensorflow实现联邦模型AlexNet on CIFAR-10
  • 原文地址:https://blog.csdn.net/zheliku/article/details/132724669
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号