`

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 the stack is empty.
Notes:
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

用两个队列来实现堆栈后进先出的功能。代码如下:
class MyStack {
    Queue<Integer> q1 = new LinkedList<Integer>();
    Queue<Integer> q2 = new LinkedList<Integer>();
    // Push element x onto stack.
    public void push(int x) {
        q1.offer(x);
    }

    // Removes the element on top of the stack.
    public void pop() {
        while(q1.size() > 1) 
            q2.offer(q1.poll());
        q1.poll();
        Queue<Integer> tem = q1;
        q1 = q2;
        q2 = tem;
    }

    // Get the top element.
    public int top() {
        while(q1.size() > 1)
            q2.offer(q1.poll());
        int result = q1.poll();
        q2.offer(result);
        Queue<Integer> tem = q1;
        q1 = q2;
        q2 = tem;
        return result;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q1.isEmpty();
    }
}
1
1
分享到:
评论

相关推荐

    225.Implement stack using queues用队列实现栈【LeetCode单题讲解系列】

    225.Implement_stack_using_queues用队列实现栈【LeetCode单题讲解系列】

    java-leetcode题解之Implement Stack using Queues.java

    在Java中实现栈(Stack)结构可以有多种方式,其中一种是使用队列(Queue)。队列是一种先进先出(FIFO)的数据结构,而栈则是一种后进先出(LIFO)的数据结构。要使用队列实现栈的功能,主要利用队列的FIFO特性以及...

    leetcode 225 Implement Stack using Queues

    leetcode 225 Implement Stack using Queues leetcode 2020年3月 每日一题打卡 思路: python 细节: 查找list中某元素的位置:list.index(i) 向下取整:int() 代码: class Solution(object): def majorityElement...

    前端大厂最新面试题-stack.docx

    5. 用队列实现栈(Implement Stack Using Queues):使用队列来实现栈的基本操作。难度:简单 6. 最小栈(Min Stack):设计一个栈,能够在 O(1) 时间内获取最小值。难度:简单 7. 删除字符串中的所有相邻重复项...

    java-leetcode面试题解Stack之第225题用队列实现栈-题解.zip

    这道题目是LeetCode中的第225题,名为"用队列实现栈"(Implement Stack using Queues)。在这个问题中,我们需要使用两个队列来模拟栈的数据结构和操作,主要包含`push`、`pop`和`top`等基本功能。 栈是一种后进先...

    算法面试通关40讲完整课件 08-10 堆栈、队列

    LeetCode上的题目如"Implement Queue using Stacks"和"Implement Stack using Queues"要求用堆栈或队列来模拟另一种数据结构,这是对数据结构灵活运用能力的考察。这样的题目有助于理解堆栈和队列的本质,并提升编程...

    python-leetcode题解之225-Implement-Stack-using-Queues.py

    知识点: 1. 题目理解: 题目225要求使用队列实现栈的功能。这属于数据结构中的常见算法题目,需要理解栈和队列的基本操作和区别。栈是一种后进先出(LIFO)的数据结构,而队列是一种先进先出(FIFO)的数据结构。...

    LeetCode最全代码

    * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap](https://github.com/kamyu104/LeetCode#heap) * [Tree]...

    leetcodepushfront-stack_w_2_queues:stack_w_2_queues

    推前stack_w_2_queues 力码 编队 /* Q. 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 ...

    leetcodepushfront-implement-stack-using-queues:使用队列实现堆栈

    stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // returns false 笔记: 您必须只使用队列的标准操作——这意味着只有向后推、从前面查看/弹出、大小和为空...

    leetcode分类-leetcode:leetcode刷题(中等难度分类)

    在解决LeetCode的问题时,还会遇到一些设计模式的应用,比如"Design HashMap"(设计哈希映射)和"Implement Stack using Queues"(用队列实现栈)。掌握设计模式可以帮助我们更好地组织代码,提高代码的可读性和复用...

    leetcodepushfront-LeetCode_225--Implement-Stack-using-Queues:LeetCode_2

    stack.empty(); // 返回假 笔记: 您必须只使用队列的标准操作——这意味着只有向后推、从前面查看/弹出、大小和为空操作是有效的。 根据您的语言,队列可能不受本机支持。 您可以使用列表或双端队列(双端队列)来...

    PHP 7 - Data Structures and Algorithms

    Implement linked lists, double linked lists, stack, queues, and priority queues using PHP ? Work with sorting, searching, and recursive algorithms ? Make use of greedy, dynamic, and pattern matching ...

    深入解析栈与队列的实现及其应用案例

    The content covers an in-depth examination of implementing stacks and queues with practical programming challenges like using a stack to implement a queue, utilizing a queue to implement a stack, ...

    leetcode296-LeetCode:https://leetcode-cn.com/problemset/all/

    leetcode 296 中文网编程题 Java 15 个人练习,部分题目整理提供了多种解法 已完成: 472个 , , , , , 6, , , , 10, , , , , , , , , , , , , , , , , , , 29, 30, , 32, , , , 36, , , , , 41, ...1

    PHP 7 Data Structures and Algorithms

    Implement linked lists, double linked lists, stack, queues, and priority queues using PHP Work with sorting, searching, and recursive algorithms Make use of greedy, dynamic, and pattern matching ...

    英文原版-Problem Solving and Program Design in C 7th Edition

    In later chapters, students learn to implement fundamental data structures such as lists, stacks, queues, and trees in a language that fosters their understanding of stack- and heap-dynamic memory ...

    Learning.JavaScript.Data.Structures.and.Algorithms.1783554878

    Understand and implement classic data structures and algorithms using JavaScript About This Book Learn how to use the most used data structures such as array, stack, list, tree, and graphs with real-...

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

Global site tag (gtag.js) - Google Analytics