LC 641. Design Circular Deque
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的更多相关文章
- LeetCode 641. Design Circular Deque
原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...
- [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- Design Circular Deque
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- Leetcode641.Design Circular Deque设计循环双端队列
设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...
- C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
随机推荐
- debian上安装tmux
1.安装ncurses库 1.1.获取源码 wget https://invisible-island.net/datafiles/release/ncurses.tar.gz tar xvf ncu ...
- 修正Calendar的Bug
procedure TAndroidNativeCalendarListener.onSelectedDayChange(view: JCalendarView; year, month, dayOf ...
- 【Struts2】文件上传与下载
一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...
- kolla-ansible 部署多region
目录 kolla-ansible 部署多region 一.前言 二.部署架构 三.部署细节 1.部署RegionOne 2.部署RegionTwo kolla-ansible 部署多region 一. ...
- P5657 格雷码
思路 考场上的递归思路 每次向下递归的时候判断是左半边还是右半边即可 注意向右半边递归之后下一层序列要反转过来即可 代码 #include <cstdio> #include <al ...
- CF901C Bipartite Segments[点双+二分+前缀优化]
不想翻译了,直接放luogu翻译 说了没有偶环,也就是说全是奇环,再结合二分图性质,那么暴力的话,固定左端点,增大序号,加点直到产生环就不合法了.也就是说,任何一个环,只要他上面的数全都被加了,就不合 ...
- LINUX查看内存使用情况 free
# free 显示结果如下: Mem:表示物理内存统计 total 内存总数 8057964KB used 已使用的内存 7852484KB free 空闲的内存数 205480KB shared 当 ...
- [译] 优化 WEBPACK 以更快地构建 REACT
原文地址:OPTIMIZING WEBPACK FOR FASTER REACT BUILDS 原文作者:Jonathan Rowny 译文出自:掘金翻译计划 本文永久链接:https://githu ...
- C#主菜单动态添加子菜单并设置触发事件
我所使用的是devxepress中的主菜单栏时barsubitem控件,想的是在其能够动态添加子菜单栏并能点击触发事件: /// <summary> /// 创建主按钮的子按钮 /// & ...
- 从ORM框架到SQLAlchemy
一.ORM 1.什么是ORM 对象-关系映射(Object-Relational Mapping,简称ORM),面向对象的开发方法是当今企业级应用开发环境中的主流开发方法,关系数据库是企业级应用环境中 ...