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 element.
- empty() -- Return whether the queue is empty.
class MyQueue { public Stack<Integer> stackPush; public Stack<Integer> stackPop; public MyQueue() { stackPush = new Stack<Integer>(); stackPop = new Stack<Integer>(); } // Push element x to the back of queue. public void push(int x) { stackPush.push(x); } // Removes the element from in front of queue. public void pop() { if (stackPop.isEmpty() && stackPush.isEmpty()) { ; } else if (stackPop.isEmpty()) { while (!stackPush.isEmpty()) { stackPop.push(stackPush.pop()); } } stackPop.pop(); } // Get the front element. public int peek() { if (stackPop.isEmpty() && stackPush.isEmpty()) { return 0; } else if (stackPop.isEmpty()) { while (!stackPush.isEmpty()) { stackPop.push(stackPush.pop()); } } return stackPop.peek(); } // Return whether the queue is empty. public boolean empty() { return stackPop.isEmpty() && stackPush.isEmpty(); } }
相关推荐
java java_leetcode题解之Implement Queue Using Stacks.java
232.Implement_Queue_using_Stacks用栈实现队列【LeetCode单题讲解系列】
4. 用栈实现队列(Implement Queue Using Stacks):使用栈来实现队列的基本操作。难度:简单 5. 用队列实现栈(Implement Stack Using Queues):使用队列来实现栈的基本操作。难度:简单 6. 最小栈(Min Stack):...
python python_leetcode题解之232_Implement_Queue_using_Stacks.py
LeetCode上的题目如"Implement Queue using Stacks"和"Implement Stack using Queues"要求用堆栈或队列来模拟另一种数据结构,这是对数据结构灵活运用能力的考察。这样的题目有助于理解堆栈和队列的本质,并提升编程...
Implement Queue using Stacks Week5 完成LeetCode 147. Insertion Sort List 完成作业Quick sort Week6 10/11 国庆弹性放假 Week7 完成作业Heap sort Week8 完成作业Merge sort Week9 Week10 完成作业Binary Search...
Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳台阶 70. Climbing Stairs 变态跳台阶 矩形覆盖 二进制中1的个数 191. Number ...
推前LeetCode_232--Implement-Queue-using-Stacks 使用堆栈实现队列的以下操作。 push(x) -- 将元素 x 推到队列的后面。 pop()——从队列前面移除元素。 peek() -- 获取最前面的元素。 empty() -- 返回队列是否为空...
* [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap](https://github.com/kamyu104/LeetCode#heap) * [Tree](https://github.com/kamyu104/LeetCode#tree) * [Hash Table]...
queue.push(1); queue.push(2); queue.peek(); // returns 1 queue.pop(); // returns 1 queue.empty(); // returns false 笔记: 您必须仅使用堆栈的标准操作——这意味着只有推到顶部、从顶部查看/弹出、大小和为...
比如“有效的括号”(Valid Parentheses)使用栈来检查括号的正确性,而“用队列实现栈”(Implement Queue using Stacks)则展示了它们的灵活应用。 3. **堆**:最大堆和最小堆常用于优先队列和优化问题。例如,...
:page_facing_up: :chart_increasing: LeetCode刷题的源代码文件 个人LeetCode主页 原始结构 所有刷过的例题的二进制都放在了源目录下面,文本文件是代码测试文件夹。 以_结尾的表示没有做出来 ...
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, ...
Table of ContentsBuilding Stacks for Application State ManagementCreating Queues for In-Order ExecutionsUsing Sets and Maps for Faster ApplicationsUsing Trees for Faster Lookup and ...
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-...
The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...