当前位置:网站首页>Leetcode stack and queue questions
Leetcode stack and queue questions
2022-07-24 02:46:00 【[email protected]】
Write it at the front : Review the basic knowledge of stack and queue , And then through leetcode To deepen our understanding of several topics , More is the use of stacks and queues , There is no specific implementation of stack and queue operations
List of articles
Topic 1
622. Design loop queue
Design your loop queue implementation . Circular queue is a linear data structure , Its operation performance is based on FIFO( fifo ) Principle and the end of the team is connected behind the head of the team to form a loop . It's also called “ Ring buffer ”.
One of the benefits of circular queues is that we can take advantage of the previously used space in this queue . In a normal queue , Once a queue is full , We can't insert the next element , There's room even in front of the queue . But with circular queues , We can use this space to store new values .
Your implementation should support the following operations :
MyCircularQueue(k): Constructors , Set queue length to k .
Front: Get elements from team leader . If the queue is empty , return -1 .
Rear: Get team end element . If the queue is empty , return -1 .
enQueue(value): Insert an element into the loop queue . True if successfully inserted .
deQueue(): Remove an element from the loop queue . True if deleted successfully .
isEmpty(): Check if the loop queue is empty .
isFull(): Check if the loop queue is full .
Question answer
matters needing attention :
- ArrayList It's a variable length array , There is no need to set the length , Usage method
ArrayList<Integer> arrayList = new ArrayList<>();
The problem can be realized by array
- Queue is tail in , Head out
In the title headIndex It represents the index of the header , So there is no need to change when joining the team headIndex Of
class MyCircularQueue {
Integer[] Q;
int headIndex;
int count;
int length;
public MyCircularQueue(int k) {
Q = new Integer[k];
headIndex = 0;
count = 0;
length = k;
}
public boolean enQueue(int value) {
if(count == length){
return false;
}
else{
Q[(headIndex + count) % length] = value;
count++;
return true;
}
}
public boolean deQueue() {
if(count == 0){
return false;
}
else{
count--;
headIndex = (headIndex +1 ) % length;
return true;
}
}
public int Front() {
if(count == 0){
return -1;
}
return Q[headIndex];
}
public int Rear() {
if(count == 0){
return -1;
}
//System.out.println((headIndex + count)%length);
return Q[(headIndex + count - 1) % length];
}
public boolean isEmpty() {
return (count == 0);
}
public boolean isFull() {
return (count == length);
}
}
/** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue obj = new MyCircularQueue(k); * boolean param_1 = obj.enQueue(value); * boolean param_2 = obj.deQueue(); * int param_3 = obj.Front(); * int param_4 = obj.Rear(); * boolean param_5 = obj.isEmpty(); * boolean param_6 = obj.isFull(); */
Topic two
232. Using stack to realize queue
Please use only two stacks to implement the FIFO queue . The queue should support all operations supported by the general queue (push、pop、peek、empty):
Realization MyQueue class :
void push(int x) Put the element x Push to the end of the queue
int pop() Remove from the beginning of the queue and return the element
int peek() Returns the element at the beginning of the queue
boolean empty() If the queue is empty , return true ; otherwise , return false
explain :
you Can only Use standard stack operations —— It's just push to top, peek/pop from top, size, and is empty Operation is legal .
The language you use may not support stacks . You can use list perhaps deque( deque ) To simulate a stack , As long as it's a standard stack operation .
Question answer
class MyQueue {
Stack<Integer> a;
Stack<Integer> b;
public MyQueue() {
a = new Stack<Integer>();
b = new Stack<Integer>();
}
public void push(int x) {
a.push(x);
}
public int pop() {
if(!b.isEmpty()){
return b.pop();
}
else{
while(!a.isEmpty()){
b.push(a.pop());
}
return b.pop();
}
}
public int peek() {
if(!b.isEmpty()){
return b.peek();
}
else{
while(!a.isEmpty()){
b.push(a.pop());
}
return b.peek();
}
}
public boolean empty() {
return a.isEmpty() && b.isEmpty();
}
}
/** * 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(); * boolean param_4 = obj.empty(); */
Topic three
225. Using queue to implement stack
Please use only two queues to implement one LIFO (LIFO) The stack , And supports all four operations of ordinary stack (push、top、pop and empty).
Realization MyStack class :
void push(int x) Put the element x Press into the top of the stack .
int pop() Remove and return the top element of the stack .
int top() Back to top of stack element .
boolean empty() If the stack is empty , return true ; otherwise , return false .
Be careful :
You can only use the basic operations of the queue —— That is to say push to back、peek/pop from front、size and is empty These operations .
The language you use may not support queues . You can use list ( list ) perhaps deque( deque ) To simulate a queue , As long as it's a standard queue operation .
Question answer
class MyStack {
Queue<Integer> Q1;
Queue<Integer> Q2;
public MyStack() {
Q1 = new LinkedList<Integer>();
Q2 = new LinkedList<Integer>();
}
public void push(int x) {
Q2.offer(x);
while(!Q1.isEmpty()){
Q2.offer(Q1.poll());
}
Queue<Integer> temp = new LinkedList<Integer>();
temp = Q1;
Q1 = Q2;
Q2 = temp;
}
public int pop() {
return Q1.poll();
}
public int top() {
return Q1.peek();
}
public boolean empty() {
return Q1.isEmpty();
}
}
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240244539304.html
边栏推荐
- 老公,我们现在无家可归了
- 攻防世界WEB练习区(view_source、get_post、robots)
- Attack and defense world web practice area (weak_auth, simple_php, xff_referer)
- X Actual combat - Cloud Server
- Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
- 云原生讲解【扩展篇】
- Mysql数据库,分组函数篇
- JpaRepository扩展接口
- 【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part2):图谱数据准备与导入
- Rylstim Screen Recorder
猜你喜欢

SSM家庭理财个人理财管理系统记账系统
![[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu](/img/be/47b8a86399f760e7cd6181528884c6.png)
[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu

SSM family financial management personal financial management system accounting system

Recommendation system topic | recommendation system architecture and single domain cross domain recall model

攻防世界WEB练习区(view_source、get_post、robots)

Wonderful! The description of meituan Octo distributed service management system is too clear

Super complete PMP reference document summary

Attack and defense world web practice area (backup, cookie, disabled_button)

云原生讲解【扩展篇】

攻防世界WEB练习区(backup、cookie、disabled_button)
随机推荐
Doodle Icons - 一组免费商用的涂鸦风格图标库,可爱轻快又独特
No coding is required, and the "asynchronous request reply" mode is automatically implemented
Correlation
stb_ Image replaces other libraries
自定义kindeditor富文本默认的宽高
About offline use of SAP Fiori application
Liveqing live broadcast on demand streaming media OBS streaming live broadcast how to obtain interface verification token video verification streamtoken and configure token validity
【补题日记】[2022牛客暑期多校1]D-Mocha and Railgun
go errors
c语言小练习
[diary of supplementary questions] [2022 Niuke summer school 1] j-serval and essay
Dynamically set the navigationbartitletext of the applet
Maximize, minimize, restore, close and move the WinForm form form of C #
相关性(correlation)
理解加载class到JVM的时机
Numberoptional: a tool for converting strings to numbers
【HDLBits 刷题】Verilog Language(2)Vectors 部分
[diary of supplementary questions] [2022 Niuke summer school 1] c-grab the seat
Doodle icons - a free commercial graffiti style icon library, cute, light and unique
【英雄哥七月集训】第 23天: 字典树