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 ...
随机推荐
- 【Git】四、本地Git的工作原理
一.工作区 第一篇创建版本库讲的有些笼统,这里详细区分一下各个概念 在第一篇我们创建了一个文件夹GitRepo,但其实这个文件夹本身并不是版本库,只是我们的工作区.我们所有的文件创建和修改都在这里执行 ...
- Linux部署tomcat服务常用命令
cd / 转到根目录 ps -ef|grep tomcat查看进程 ./bin/shutdown.sh 关闭 rm -r logs/* 清除日志 ps -ef|grep tomcat查看进程 ./bi ...
- 用例图,ER图,架构图
用例图 ER图 架构图 注:附上小组画图文档链接 提取码:t7ij v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VM ...
- PXC节点启动与关闭
PXC节点启动与关闭 最后关闭的PXC节点是安全退出时. cat /var/lib/mysql/grastate.dat,其中safe_to_bootstrap: 1,再次启动集群是则先启动该节点 s ...
- 获取select的值
<!-- html --> <select id=''check> <option>北京</option> <option>北京</o ...
- 2.01_Python网络爬虫概述
一:什么是网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取网络信息的程序或者脚本: 二:为什么要做网络爬虫? 大数据时代 ...
- PAT Basic 1057 数零壹 (20 分)
给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定 ...
- Spring事务管理----------整合学习版
作者:学无先后 达者为先 Spring提供了一流的事务管理.在Spring中可以支持声明式事务和编程式事务. 一 spring简介 1 Spring的事务 事务管理在应用程序中起着至关重 ...
- 计数dp做题笔记
YCJS 3924 饼干 Description 给定一个长度为\(n\)的序列,序列每个元素的取值为\([1,x]\),现在给定\(q\)个区间,求在所有取值方案中,区间最小值的最大值的期望是多少? ...
- Nginx 转发特点URL到指定服务
location ^~ /fs/ {#如https://xx.com/fs/upload 转发到文件服务器 proxy_pass http://127.0.0.1:8080/fs/; }