641. 设计循环双端队列
设计实现双端队列。
你的实现需要支持以下操作:
- MyCircularDeque(k):构造函数,双端队列的大小为k。
- insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。
- insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
- deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。
- deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
- getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
- getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。
- isEmpty():检查双端队列是否为空。
- isFull():检查双端队列是否满了。
示例:
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3 circularDeque.insertLast(1); // 返回 true circularDeque.insertLast(2); // 返回 true circularDeque.insertFront(3); // 返回 true circularDeque.insertFront(4); // 已经满了,返回 false circularDeque.getRear(); // 返回 2 circularDeque.isFull(); // 返回 true circularDeque.deleteLast(); // 返回 true circularDeque.insertFront(4); // 返回 true circularDeque.getFront(); // 返回 4
提示:
- 所有值的范围为 [1, 1000]
- 操作次数的范围为 [1, 1000]
- 请不要使用内置的双端队列库。
class MyCircularDeque {
public:
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
size = k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if (isFull())
return false;
data.insert(data.begin(), value);
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull()) return false;
data.push_back(value);
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty()) return false;
data.erase(data.begin());
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty()) return false;
data.pop_back();
return true;
}
/** Get the front item from the deque. */
int getFront() {
if (isEmpty()) return -1;
return data.front();
}
/** Get the last item from the deque. */
int getRear() {
if (isEmpty()) return -1;
return data.back();
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
return data.empty();
}
/** Checks whether the circular deque is full or not. */
bool isFull() {
return data.size() >= size;
}
private:
vector<int> data;
int size;
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* bool param_1 = obj.insertFront(value);
* bool param_2 = obj.insertLast(value);
* bool param_3 = obj.deleteFront();
* bool param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* bool param_7 = obj.isEmpty();
* bool param_8 = obj.isFull();
*/
641. 设计循环双端队列相关推荐
- leetcode:641. 设计循环双端队列
641. 设计循环双端队列 来源:力扣(LeetCode) 链接: https://leetcode.cn/problems/design-circular-deque/ 设计实现双端队列. 实现 M ...
- LeetCode 641. 设计循环双端队列
文章目录 1. 题目信息 2. 解题 1. 题目信息 设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront(): ...
- leetcode 622. Design Circular Queue | 641. 设计循环双端队列(Java)
题目 https://leetcode.com/problems/design-circular-deque/ 题解 相关问题:leetcode 622. Design Circular Queue ...
- LeetCode实战:设计循环双端队列
题目英文 Design your implementation of the circular double-ended queue (deque). Your implementation shou ...
- 《恋上数据结构第1季》队列、双端队列、循环队列、循环双端队列
队列(Queue) 队列 Queue 队列的接口设计 队列源码 双端队列 Deque 双端队列接口设计 双端队列源码 循环队列 Circle Queue 循环队列实现 索引映射封装 循环队列 – %运 ...
- C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...
- sv队列和动态数组的区别_Go 刷 LeetCode 系列:经典(7) 设计双端队列
设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k.insertFront():将一个元素添加到双端队列头部.如果操作成功返回 true ...
- 【数据结构】队列-顺序队列、循环队列、链队、双端队列
定义 队列是只允许在一端进行插入,而在另一端进行删除的线性表. 队头(Front):允许删除的一端,又称为队首. 队尾(Rear): 允许插入的一端. 先进入队列的元素必然先离开队列,即先进先出(Fi ...
- 【Python养成】:案例(设计三维向量类、实现向量的加法、减法以及向量与标量的乘法和除法运算、编写自定义类,模拟内置集、编写自定义类,模拟双端队列。)
学习内容:设计三维向量类.实现向量的加法.减法以及向量与标量的乘法和除法运算 设计三维向量类.实现向量的加法.减法以及向量与标量的乘法和除法运算 实验代码: class Vector_3D:def _ ...
- 单向队列、双端队列、栈的模型实现
引言 自己实现简单的队列.栈的逻辑结构. 队列都包含头和尾两个指针,简单的单向队列只能在一端(如:head端)入列,在另一端(如:tail 端)出列:双端队列可以在 head 进出,也可以在 tail ...
最新文章
- druid seata 配置_五分钟体验分布式事务框架Seata
- linux centos 网络设置 优先使用ipv4 其次ipv6
- ieee33节点系统图_【学术聚焦】考虑信息耦合的电气综合能源系统韧性优化方法...
- 开放下载 | 《Knative 云原生应用开发指南》开启云原生时代 Serverless 之门
- java file类包_Java中File类的常用API
- Arduino总结一
- mysql远程访问 linux_Linux中开启mysql远程访问功能
- FreeModbus保持寄存器
- python中如何统计元组中元素的个数_python-无论元素顺序如何,获取列表中的元组数...
- 基于mapreduce的购物篮分析算法实现
- vue 拷贝 数组_vue源码中值得学习的方法
- java vm 参数及设置(转载)
- 路由控制配置route-policy命令解析
- [Irving]Android 常用布局之RelativeLayout
- 【BZOJ4522】密匙破解(Pollard_rho)
- ASP.NET Web API实现简单的文件下载与上传
- CSAPP HITICS 大作业 hello's P2P by zsz
- 高仿京东商城app、集成react-native热更新功能
- 教育机构课程顾问常见黑话大全
- [GXOI/GZOI2019]逼死强迫症