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 ...
随机推荐
- python3 中的cls和self的区别 静态方法和类方法的区别
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某 ...
- IO 理论 SOCK理论
IO密集型程序 在程序执行过程中存在大量IO操作,而CPU操作较少,消耗CPU较少,运行效率较低CPU(计算)密集型程序 在程序执行中,CPU运算较多,IO操作相对较少(消耗CPU大,运行速度快)IO ...
- 利用commands模块执行shell命令
利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...
- ContextMenu菜单创建 上下文菜单的基本认识q
MainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void on ...
- Sublime text3安装
一.Sublime text3下载 [20190506]下载 官网下载:https://www.sublimetext.com/ https://download.sublimetext.com/Su ...
- 牛客练习赛51 C 勾股定理 (数学,结论)
链接:https://ac.nowcoder.com/acm/contest/1083/C来源:牛客网 题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角 ...
- 关于从入 OI 以来学的各种知识点的系统总结
前言 OI 之路差不多快结束了,最近水平也萎得很厉害,这里就开个目录,记录一些需要总结的知识点吧.不定期更,勿催,我还要改模拟赛的题. 目录
- javascript弹出带文字信息的提示框效果
// position of the tooltip relative to the mouse in pixel // <html><head><meta charse ...
- Vue入门(二)——Demo
1.在工程目录下创建项目 右键GIT BASH 2.安装lib(建议使用淘宝镜像安装) 3.首页 App.vue <template> <el-container> <e ...
- RabbitMQ与Spring集成配置
1.引入相关jar包 //RabbitMQ compile group: 'org.springframework.amqp', name: 'spring-rabbit', version: '1. ...