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 ...
随机推荐
- Rabbitmq各方法的作用详解
exchange_declare('direct_logs', 'direct', false, false, false);// 这个是申明交换器,如果没有申明就给默认队列的这个交换器,而且发送的类 ...
- Shell中比较判断
一.shell判断数组中是否包含某个元素:ary=(1 2 3)a=2if [[ "${ary[@]}" =~ "$a" ]] ; then echo & ...
- functools.lru_cache装饰器
functools.lru_cache装饰器 functools.lru_cache是非常实用的装饰器,他实现了备忘功能它把耗时的函数的结果保存起来,避免传入相同的参数时重复计算.LRU是Least ...
- mount -t proc none /proc
linux initrd里的init脚本中的第一句为: mount -t proc /proc /proc 作用是把proc这个虚拟文件系统挂载到/proc目录.这说明initrd需要用到/proc, ...
- web开发:javascript操作
一.函数闭包 二.面向对象 三.js选择器 四.事件初始 五.js处理页面内容 六.js事件控制标题栏 七.js控制类名 一.函数闭包 ```js// 函数的嵌套定义, 定义在内部的函数就称之为 闭包 ...
- JDBC连接数据库报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated.
使用JDBC连接数据库时出现报错, 报错内容:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver cla ...
- ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval
ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...
- solrcloud2
分片的原因 由于底层Lucene的限制,每个solr索引中包含的文档数不能超过231个,大约是21亿个.但是solr分片一般不是基于这个的原因,因为一般没有到这个峰值的之后,solr的各中性能问题就暴 ...
- BZOJ4886 [Lydsy1705月赛]叠塔游戏[基环树]
很妙的一道题. 由于本人过于zz,不会这道题,通过厚颜无耻翻阅题解无数终于懂了这道题,所以这里转载一位神仙的blog. 没有看懂?没事,再来一篇. 这题个人认为主要在于转化题意和建图,这两点想通了应该 ...
- lnmp配置
yum源切换 下载wegt工具 yum install -y wget 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...