首页 > 其他分享 >【LeetCode 0232】【设计】用FILO栈实现FIFO队列

【LeetCode 0232】【设计】用FILO栈实现FIFO队列

时间:2024-07-03 23:56:03浏览次数:3  
标签:return 0232 FIFO queue MyQueue pop outputStack push FILO

  1. Implement Queue using Stacks

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 queue.* int pop() Removes the element from the front of the queue and returns it.* int peek() Returns the element at the front of the queue.* boolean empty() Returns true if the queue is empty, false otherwise.

Notes:* You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.* Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.

Example 1:

**Input**
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
**Output**
[null, null, null, 1, 1, false]

**Explanation**
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

Constraints:* 1 <= x <= 9* At most 100 calls will be made to push, pop, peek, and empty.* All the calls to pop and peek are valid.

Follow-up: Can you implement the queue such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.

JavaScript Solution
var MyQueue = function() {
    this.inputStack = []
    this.outputStack = []
};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.inputStack.push(x)
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if(!this.outputStack.length){
        while(this.inputStack.length){
            this.outputStack.push(this.inputStack.pop())
        }
    }
    if(!this.outputStack.length){
        return null
    }
    return this.outputStack.pop()
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    if(!this.outputStack.length){
        while(this.inputStack.length){
            this.outputStack.push(this.inputStack.pop())
        }
    }
    if(!this.outputStack.length){
        return null
    }
    return this.outputStack[this.outputStack.length-1]
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return !this.inputStack.length && !this.outputStack.length
};

/** 
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

标签:return,0232,FIFO,queue,MyQueue,pop,outputStack,push,FILO
From: https://blog.csdn.net/avenccssddnn/article/details/140162081

相关文章

  • 用verilog/systemverilog 设计fifo (2)
    目录异步fifo实现中要解决的问题信号同步到那个时钟域读写指针转化为格雷码格雷码表示的读写地址如何判断空满?异步fifoverilog代码异步fifo实现中要解决的问题异步fifo和同步fifo功能相似,但是它的读写由两个时钟信号控制,所以它的设计和同步fifo不同,需要考虑更多的因素。信号......
  • FIFO in C
    /*fifo.c Description:ImplementsaFIFObufferLicense:RevisedBSDLicense,seeLICENSE.TXTfileincludeintheprojectMaintainer:MiguelLuisandGregoryCristian*/#include"fifo.h"staticuint16_tFifoNext(Fifo_t*fifo,uint16_tind......
  • 【操作系统】pipe&mkfifo|管道详解
     ......
  • 深入解析 Cognex VisionPro 的 CogAcqFifoTool
    深入解析CognexVisionPro的CogAcqFifoTool在现代工业自动化和机器视觉领域,图像获取是实现各种视觉检测、识别和分析的第一步。而CognexVisionPro提供了一系列强大的工具,其中CogAcqFifoTool是专门用于图像获取的重要工具。本文将深入解析CogAcqFifoTool,帮助您了解其功......
  • 用verilog/systemverilog 设计fifo (1)
    目录fifo的基本原理基于计数器的同步fifo实现(1)基于计数器的同步fifo实现(2)基于高位补偿法的fifo实现fifo的基本原理FIFO(firstinfirstout),即先进先出存储器,功能与数据结构中的队列相似。在IC设计中,FIFO常用来缓冲突发数据,流式数据与块数据的转换等等。比如上图中,在两个......
  • (4)跨时钟域设计(多bit+FIFO)
    一、引入 以上是多bit指示信号的传输与指示信号不同,多bit数据流具有连续性,即背靠背传输,同时要求信号具有较快的传播速度目前多bit数据流传输有两种,一种是借助SRAM,另一种是借助FIFO二、FIFO 如果FIFO内数据写满则生成满信号,反压上游结点,上游停止写入新......
  • ibatis-FifoCache
    核心代码Deque<Object>keyList=newLinkedList<>();为什么使用LinkedList?单向链表。使用LinkedList实现FIFO,支持头、尾节点的单向链表。添加时,判断数量大于初始化值时,删除头结点。源码:publicclassFifoCacheimplementsCache{privatefinalCachedelegate;pri......
  • 题解:SP10232 AMR11E - Distinct Primes
    前话这咋人名都和HP一模一样了,SPOJ出题人里是不是全是哈迷啊。思路非常直观的一个思路:从前往后枚举每一个数,看是否满足条件,输出满足条件的第一个。CODE#include<bits/stdc++.h>usingnamespacestd;boolis(intn){//判断质数if(n<2)return0;for(inti=2;i<......
  • P10232 [COCI 2023/2024 #4] Roboti 题解
    P10232[COCI2023/2024#4]Roboti题解知识点简单环,DFS。题意分析在\(n\)行,\(m\)列的网格里,给定\(k\)个转弯点,再给定\(Q\)个询问,问每次从某个坐标到另一个坐标的最少转弯次数,或者判断不可能到达。思路分析我们发现在一个点坐标与方向确定的时候,到达的下一个点的......
  • S3-FIFO
    S3-FIFO本文作为下一篇缓存文章的预备知识。背景基于LRU和FIFO的驱逐FIFO和LRU都是经典的缓存驱逐算法,在过去几十年中也出现了很多追求更高效率的驱逐算法,如ARC,2Q,LIRS,TinyLFU。传统观点认为,基于LRU的缓冲未命中率要低于基于FIFO的算法,如CLOCK,这类高级算法通常都是基于LR......