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

Your implementation should support following operations:

  • MyCircularDeque(k): Constructor, set the size of the deque to be k.
  • insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
  • insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
  • deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
  • deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
  • getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
  • getRear(): Gets the last item from Deque. If the deque is empty, return -1.
  • isEmpty(): Checks whether Deque is empty or not.
  • isFull(): Checks whether Deque is full or not.

Example:

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1); // return true
circularDeque.insertLast(2); // return true
circularDeque.insertFront(3); // return true
circularDeque.insertFront(4); // return false, the queue is full
circularDeque.getRear(); // return 2
circularDeque.isFull(); // return true
circularDeque.deleteLast(); // return true
circularDeque.insertFront(4); // return true
circularDeque.getFront(); // 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 Deque library.
 

Runtime: 112 ms, faster than 32.91% of Java online submissions for Design Circular Deque.

class MyCircularDeque {
private Deque<Integer> dq = new LinkedList<>();
private int cap;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
cap = k;
} /** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(dq.size() == cap) return false;
dq.offerFirst(value);
return true;
} /** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(dq.size() == cap) return false;
dq.offerLast(value);
return true;
} /** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if(dq.size() == ) return false;
dq.removeFirst();
return true;
} /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if(dq.size() == ) return false;
dq.removeLast();
return true;
} /** Get the front item from the deque. */
public int getFront() {
if(dq.size() == ) return -;
return dq.peekFirst();
} /** Get the last item from the deque. */
public int getRear() {
if(dq.size() == ) return -;
return dq.peekLast();
} /** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return dq.size() == ;
} /** Checks whether the circular deque is full or not. */
public boolean isFull() {
return dq.size() == cap;
}
}

LC 641. Design Circular Deque的更多相关文章

  1. LeetCode 641. Design Circular Deque

    原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...

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

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

  3. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  4. [Swift]LeetCode641. 设计循环双端队列 | 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. Design Circular Deque

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

  7. Leetcode641.Design Circular Deque设计循环双端队列

    设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...

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

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

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

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

随机推荐

  1. mock.js学习之路一(Vue中使用)

    1.安装mockjs 2.配置mockjs在开发环境中启用,生产环境中禁用 3.创建mock文件夹,以及mock数据文件 4.在main.js中引入与否 5.页面获取数据 testMock(){ th ...

  2. 【python】多进程、多线程、序列

    一.多进程 1.子进程永远返回0,而父进程返回子进程的ID.这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程 ...

  3. 前端基础(二):CSS

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...

  4. 关于JPype报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误的解决

    部署到线上的项目正常运行一年,今天早上突然报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误. JPyp ...

  5. Visual Studio中使用GitHub

    一.简介 GitHub被微软收购后免费开放了私人仓库,这对个人或小型团队来说简直是福音.GitHub高效免费,无论是团队代码管理还是DevOps都不再话下,特别是对于想要开放研究成果提高知名度的团队或 ...

  6. 用命令行的方式把jmeter结果文件JTL生成csv格式的聚合报告

    我们知道 利用jmeter 的GUI的 Aggragate Listner 很容易把一个JTL 文件另存为CSV 文件,该CSV 文件中自动分析了 Transactions 的 90%, Median ...

  7. Matlab---绘制柱状图

    Matlab---绘制柱状图 目录: hist()函数 histc( )函数 bar()函数 正文: 注意区分:频率.频数分布直方图. 一.             hist()函数 hist():实 ...

  8. 【概率dp】vijos 3747 随机图

    没有养成按状态逐步分析问题的思维 题目描述 在一张图内,两点$i,j$之间有$p$的概率的概率生成一条边.求该图不出现大小$\ge 4$连通块的概率. $n \le 100,答案在实数意义下$ 题目分 ...

  9. invalid argument "type=bind,source=/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_cpu," for "--mount" flag: invalid field '' must be a key=value pair

    --mount参数后面不要有空格 空格去掉即可

  10. DNS服务基础

    DNS服务器的功能 – 正向解析:根据注册的域名查找其对应的IP地址 – 反向解析:根据IP地址查找对应的注册域名(不常用) NS(声明DNS记录) A(正向解析记录) CNAME(解析记录别名) 安 ...