设计实现双端队列。

你的实现需要支持以下操作:

  • 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(); // 返回 32 circularDeque.isFull(); // 返回 true circularDeque.deleteLast(); // 返回 true circularDeque.insertFront(4); // 返回 true circularDeque.getFront(); // 返回 4

提示:

  • 所有值的范围为 [1, 1000]
  • 操作次数的范围为 [1, 1000]
  • 请不要使用内置的双端队列库。
class MyCircularDeque {
public:
int front;
int rear;
int count;
vector<int> mydeque;
int size;
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
front = 0;
rear = 0;
count = 0;
mydeque = vector<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;
front = front == 0? size - 1 : front - 1;
mydeque[front] = value;
count++;
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;
mydeque[rear] = value;
rear = (rear + 1) % size;
count++;
return true;
} /** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if(isEmpty())
return false;
front = (front + 1) % size;
count--;
return true;
} /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if(isEmpty())
return false;
rear = rear == 0? size - 1 : rear - 1;
count--;
return true;
} /** Get the front item from the deque. */
int getFront() {
if(isEmpty())
return -1;
return mydeque[front];
} /** Get the last item from the deque. */
int getRear() {
if(isEmpty())
return -1;
return rear == 0? mydeque[size - 1] : mydeque[rear - 1];
} /** Checks whether the circular deque is empty or not. */
bool isEmpty() {
if(count == 0)
return true;
return false;
} /** Checks whether the circular deque is full or not. */
bool isFull() {
if(count == size)
return true;
return false;
}
};

Leetcode641.Design Circular Deque设计循环双端队列的更多相关文章

  1. Java实现 LeetCode 641 设计循环双端队列(暴力)

    641. 设计循环双端队列 设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头 ...

  2. [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  3. C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...

  4. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  5. [LeetCode] Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  6. Leetcode622.Design Circular Queue设计循环队列

    设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列的一个好处是 ...

  7. java数据结构-11循环双端队列

    @SuppressWarnings("unchecked") public class CircleDeque<E> { private int front; priv ...

  8. 队列(Queue)\双端队列(Deque)

    队列(Queue)\双端队列(Deque) 队列(Queue) 双端队列(Deque) 算法应用 队列(Queue) 特点: 和栈不同,队列的最大特点是先进先出(FIFO),就好像按顺序排队一样.对于 ...

  9. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

随机推荐

  1. 深入浅出 Java Concurrency (33): 线程池 part 6 线程池的实现及原理 (1)[转]

    线程池数据结构与线程构造方法 由于已经看到了ThreadPoolExecutor的源码,因此很容易就看到了ThreadPoolExecutor线程池的数据结构.图1描述了这种数据结构. 图1 Thre ...

  2. Sql Server 中查询存储过程的修改时间

    1.按最近修改排序所有存储过程 SELECT [name], [create_date], [modify_date] FROM [sys].[objects] WHERE [type] = 'P' ...

  3. <每日一题>题目20:简单python练习题(11-20)

    #11.编写程序,输入一个自然数,输出它的二进制.八进制.十六进制表示形式 Num = input("请输入任性自然数:") Num = eval(Num) print(" ...

  4. DROOLS通过URL访问changset

    package droolsRule; import java.net.Authenticator; import java.net.PasswordAuthentication; import ka ...

  5. Leetcode944. Delete Columns to Make Sorted删列造序

    给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符. 所余下的字符串行从上往下读形成列. 比如,有 A = ...

  6. exit()和return语句的区别

    (1)exit用于结束正在运行的程序,exit函数将参数是返回给OS.而return是返回函数值并退出函数. (2)return是语言级别的,它表示了调用堆栈的返回:而exit是系统调用级别的,它表示 ...

  7. ubuntu触摸板失效问题

    很早便遇到这个问题,今天忍无可忍才度娘了一发.亲测有效! 用Ubuntu Tweak备份过桌面的配置,因此我尝试恢复桌面设置,果然奇迹发生了,触摸板立刻恢复了正常使用! 没有备份过的相信使用其中的重置 ...

  8. jeecms 前台拦截器的研究与改造

    jeecms 前台拦截器的研究与改造 2013年12月24日 15:23:35 xinfei0803 阅读数 3511   jeecms出发点是面向大众的,具有前台开发性,也就是说,即时是未登录(游客 ...

  9. 使用em为单位制作两列弹性布局

    一.DIV布局按照定位的方法分为:浮动方法(float),坐标定位方法(position),还有就是两者相结合的方法. 二.DIV布局按照定义单位的不同可分为:固定宽度布局.流体布局.弹性布局和混合布 ...

  10. Jmeter分布式测试笔记

    在性能测试过程中,如果要求并发数较大时(例如1000+),单机配置cpu与内存等无法支持,则需要使用Jmeter的分布式测试方法. 一.一般什么情况下需要分布式 1.前辈经验:比如机器i5双核的cpu ...