[LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Your implementation should support following operations:
MyCircularQueue(k)
: Constructor, set the size of the queue to be k.Front
: Get the front item from the queue. If the queue is empty, return -1.Rear
: Get the last item from the queue. If the queue is empty, return -1.enQueue(value)
: Insert an element into the circular queue. Return true if the operation is successful.deQueue()
: Delete an element from the circular queue. Return true if the operation is successful.isEmpty()
: Checks whether the circular queue is empty or not.isFull()
: Checks whether the circular queue is full or not.
Example:
MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
circularQueue.enQueue(1); // return true
circularQueue.enQueue(2); // return true
circularQueue.enQueue(3); // return true
circularQueue.enQueue(4); // return false, the queue is full
circularQueue.Rear(); // return 3
circularQueue.isFull(); // return true
circularQueue.deQueue(); // return true
circularQueue.enQueue(4); // return true
circularQueue.Rear(); // return 4
Note:
- All values will be in the range of [0, 1000].
- The number of operations will be in the range of [1, 1000].
- Please do not use the built-in Queue library.
设计一个环形队列,包含以下几个操作功能:设置队列大小,获取前一个元素,获取最后一个元素,插入一个元素,删除一个元素,判断队列是否满了,判断队列是否为空。不能用内置的Queue函数库。
解法:数组
Java:
class MyCircularQueue {
final int[] a;
int front, rear = -1, len = 0; public MyCircularQueue(int k) { a = new int[k];} public boolean enQueue(int val) {
if (!isFull()) {
rear = (rear + 1) % a.length;
a[rear] = val;
len++;
return true;
} else return false;
} public boolean deQueue() {
if (!isEmpty()) {
front = (front + 1) % a.length;
len--;
return true;
} else return false;
} public int Front() { return isEmpty() ? -1 : a[front];} public int Rear() {return isEmpty() ? -1 : a[rear];} public boolean isEmpty() { return len == 0;} public boolean isFull() { return len == a.length;}
}
Python:
class Node:
def __init__(self, value):
self.val = value
self.next = self.pre = None
class MyCircularQueue: def __init__(self, k):
self.size = k
self.curSize = 0
self.head = self.tail = Node(-1)
self.head.next = self.tail
self.tail.pre = self.head def enQueue(self, value):
if self.curSize < self.size:
node = Node(value)
node.pre = self.tail.pre
node.next = self.tail
node.pre.next = node.next.pre = node
self.curSize += 1
return True
return False def deQueue(self):
if self.curSize > 0:
node = self.head.next
node.pre.next = node.next
node.next.pre = node.pre
self.curSize -= 1
return True
return False def Front(self):
return self.head.next.val def Rear(self):
return self.tail.pre.val def isEmpty(self):
return self.curSize == 0 def isFull(self):
return self.curSize == self.size
C++:
class MyCircularQueue {
public:
/** Initialize your data structure here. Set the size of the queue to be k. */
MyCircularQueue(int k) {
data.resize(k);
head = 0;
tail = 0;
reset = true;
} /** Insert an element into the circular queue. Return true if the operation is successful. */
bool enQueue(int value) {
if (isFull()) return false;
// update the reset value when first enqueue happens
if (head == tail && reset) reset = false;
data[tail] = value;
tail = (tail + 1) % data.size();
return true;
} /** Delete an element from the circular queue. Return true if the operation is successful. */
bool deQueue() {
if (isEmpty()) return false;
head = (head + 1) % data.size();
// update the reset value when last dequeue happens
if (head == tail && !reset) reset = true;
return true;
} /** Get the front item from the queue. */
int Front() {
if (isEmpty()) return -1;
return data[head];
} /** Get the last item from the queue. */
int Rear() {
if (isEmpty()) return -1;
return data[(tail + data.size() - 1) % data.size()];
} /** Checks whether the circular queue is empty or not. */
bool isEmpty() {
if (tail == head && reset) return true;
return false;
} /** Checks whether the circular queue is full or not. */
bool isFull() {
if (tail == head && !reset) return true;
return false;
}
private:
vector<int> data;
int head;
int tail;
// reset is the mark when the queue is empty
// to differentiate from queue is full
// because in both conditions (tail == head) stands
bool reset;
}; /**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* bool param_1 = obj.enQueue(value);
* bool param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* bool param_5 = obj.isEmpty();
* bool param_6 = obj.isFull();
*/
类似题目:
[LeetCode] 641.Design Circular Deque 设计环形双向队列
All LeetCode Questions List 题目汇总
[LeetCode] 622.Design Circular Queue 设计环形队列的更多相关文章
- [LeetCode] Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- Leetcode622.Design Circular Queue设计循环队列
设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列的一个好处是 ...
- LeetCode 622. Design Circular Queue
原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...
- [LeetCode] Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【leetcode】622. Design Circular Queue
题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...
- LeetCode 622:设计循环队列 Design Circular Queue
LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...
- LeetCode 641. Design Circular Deque
原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...
随机推荐
- Linux下g++编译thread出错的的解决方法
错误如下图所示: 因为thread是C++11新加入的特性,所以我们在用g++编译的时候不能直接用,需要在g++后面加上 -std=c++0x -pthread 如果是gcc编译多线程的话则应该要用 ...
- 50: Luogu P4568 分层图
分层图最短路模板 #include <iostream> #include <cstdio> #include <cstdlib> #include <cti ...
- cf 1179 C
目录 A B C A 模拟出A不是最大值的情况,存起来. 最多有n个.当A为最大值的时候,后面n-1个数开始循环. 查询分两种情况讨论就行了 #include <bits/stdc++.h> ...
- 洛谷P2949题解
若想要深入学习反悔贪心,传送门. Description: 有 \(n\) 项工作,每 \(i\) 项工作有一个截止时间 \(D_i\) ,完成每项工作可以得到利润 \(P_i\) ,求最大可以得到多 ...
- X-factor Chain(信息学奥赛一本通 1628)
题目描述 输入正整数 x,求 x 的大于 1 的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及满足最大长度的序列的个数. 输入 多组数据,每组数据一行,包含一个正整数 x. 对于全部数据 ...
- linux高性能服务器编程 (五) --Linux网络编程基础api
第五章 Linux网络编程基础api 1.主机字节序和网络字节序 字节序是指整数在内存中保存的顺序.字节序分为大端字节序.小端字节序. 大端字节序:一个整数的高位字节数据存放在内存的低地址处.低位字节 ...
- linux jar/war包 后台运行
1. 基础版,当前ssh窗口锁定,按CTRL+C打断程序运行:或关闭窗口,程序退出 java -jar flowable-modeler.war 2. 改进版,当前ssh窗口不锁定,窗口关闭时,程序终 ...
- 简单find命令的实现
贴代码: /*实现一个简单的find命令:*//*程序思路:首先,用一个单链表将所需要的信息存储起来:其次根据所传入的参数信息,改变节点的状态(若有这个状态,证明该节点就是我们所需要的)最后将所需要的 ...
- 方法型混淆js代码
const fs = require('fs'); const acorn = require('acorn'); const walk = require("acorn-walk" ...
- 是什么让我走上Java之路?
选择方向,很多人都为根据自己的兴趣爱好和自己的能力所长而作出选择.那么是什么让我走上Java之路? 整个高三我有两门课程没有听过课,一门是数学,一门是物理.当时候物理没有听课的原因很简单,我有一本&l ...