- 浏览: 183660 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
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, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
用堆栈实现队列的功能,这里我们借助两个堆栈来模拟队列的先进先出的特性。代码如下:
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.
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, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
用堆栈实现队列的功能,这里我们借助两个堆栈来模拟队列的先进先出的特性。代码如下:
class MyQueue { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); // Push element x to the back of queue. public void push(int x) { stack1.push(x); } // Removes the element from in front of queue. public void pop() { while(stack1.size() > 1) { stack2.push(stack1.pop()); } stack1.pop(); while(!stack2.isEmpty()) stack1.push(stack2.pop()); } // Get the front element. public int peek() { while(stack1.size() > 1) { stack2.push(stack1.pop()); } int result = stack1.peek(); while(!stack2.isEmpty()) stack1.push(stack2.pop()); return result; } // Return whether the queue is empty. public boolean empty() { return stack1.isEmpty(); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 498Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 675Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 844Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 705For a undirected graph with tre ...
相关推荐
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...