当前位置:网站首页>LeetCode-栈和队列刷题
LeetCode-栈和队列刷题
2022-07-24 02:45:00 【[email protected]】
写在前面:复习完栈和队列的基础知识,然后通过leetcode的几道题目加深理解,更多的是对于栈和队列的运用,而没有对于栈和队列操作的具体实现
文章目录
题目一
622. 设计循环队列
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
题目解答
注意事项:
- ArrayList 是变长数组,不需要设置长度,使用方法
ArrayList<Integer> arrayList = new ArrayList<>();
该题采用数组来实现即可
- 队列是尾部进,头部出
题目中的headIndex表示的是头部的索引,所以入队时是不需要变化headIndex的
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(); */
题目二
232. 用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
题目解答
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(); */
题目三
225. 用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
题目解答
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://blog.csdn.net/Little_jcak/article/details/125901240
边栏推荐
- Composition API (in setup) watch usage details
- Relational expression greater than > less than < congruence = = = Nan isnan() logical operator double sense exclamation point!! & |% +-- Short circuit calculation assignment expression shortcut operat
- Make life full of happiness
- Vscade connects to the server. The password is correct, but it has been unable to connect
- Reading notes: self cultivation of programmers - Chapter 3
- QT display Chinese garbled code
- Maximize, minimize, restore, close and move the WinForm form form of C #
- 【FPGA教程案例38】通信案例8——基于FPGA的串并-并串数据传输
- Camper recruitment | AI youth with the world in mind, the United Nations needs your help for sustainable development!
- How to judge null for different types of fields, sets, lists / sets / maps, and objects
猜你喜欢

Unity timeline tutorial

compostion-api(setup中) watch使用细节

Interpretation of steam education with the deepening of educational reform

Relational expression greater than > less than < congruence = = = Nan isnan() logical operator double sense exclamation point!! & |% +-- Short circuit calculation assignment expression shortcut operat

让生活充满幸福感

软考---程序设计语言基础(上)

Crop leaf disease identification system

SSM family financial management personal financial management system accounting system

Jina AI and datawhale jointly launched a learning project!

SSM's technical forum includes front and back offices
随机推荐
Share an API Gateway project based on ABP and yarp
C from zero
Jina AI and datawhale jointly launched a learning project!
Do securities companies really have principal guaranteed financial products?
Data conversion problem in Qt development serial communication software: when reading_ Qbytearray to string; When sending_ Data format; Int to hexadecimal format string; Intercept characters in string
[management / upgrade] * 02. View the upgrade path * FortiGate firewall
通用机环境下安全版单机数据库使用非root用户管理的解决方案
X Actual combat - Cloud Server
云原生讲解【扩展篇】
redis数据类型概念
How to judge null for different types of fields, sets, lists / sets / maps, and objects
go log包
微信小程序实现折线面积图-玫瑰图-立体柱状图
Make life full of happiness
22 -- 二叉搜索树的范围和
[diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu
Mysql database, sorting and single line processing functions
Mysql database, query
【补题日记】[2022牛客暑期多校1]J-Serval and Essay
SSM family financial management personal financial management system accounting system