LeetCode 641. Design Circular Deque
原题链接在这里:https://leetcode.com/problems/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.
题解:
Use an array to maintain values.
start index pointing to queue head, initialized as -1.
end index pointing to queue tail, initialized as -1.
When insertFront, if start == -1, assign start as 0, else start = (start - 1 + k) % k. Assign new value to arr[start]. If end is -1, need to update end = start. This only happens at the beginning.
When insertLast, end = (end + 1) % k. Assign new value to arr[end]. If start is -1, need to update start = end. This only happends at the begining.
deleteFront, move start + 1.
deleteLast, move end - 1.
Time Complexity: MyCircularDeque, O(1). insertFront, O(1). insertLast, O(1). deleteFront, O(1). deleteLast, O(1). getFront, O(1). getRear, O(1). isEmpty, O(1). isEmpty, O(1).
Space: O(k).
AC Java:
class MyCircularDeque {
int [] arr;
int start;
int end;
int len;
int k;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
arr = new int[k];
start = -1;
end = -1;
len = 0;
this.k = k;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if(isFull()){
return false;
}
if(start == -1){
start = 0;
}else{
start = (start - 1 + k) % k;
}
arr[start] = value;
if(end == -1){
end = start;
}
len++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if(isFull()){
return false;
}
end = (end + 1) % k;
arr[end] = value;
if(start == -1){
start = end;
}
len++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if(isEmpty()){
return false;
}
start = (start + 1) % k;
len--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if(isEmpty()){
return false;
}
end = (end - 1 + k) % k;
len--;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
return isEmpty() ? -1 : arr[start];
}
/** Get the last item from the deque. */
public int getRear() {
return isEmpty() ? -1 : arr[end];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return len == 0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return len == k;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* boolean param_1 = obj.insertFront(value);
* boolean param_2 = obj.insertLast(value);
* boolean param_3 = obj.deleteFront();
* boolean param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* boolean param_7 = obj.isEmpty();
* boolean param_8 = obj.isFull();
*/
LeetCode 641. Design Circular Deque的更多相关文章
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- LC 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 ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- LeetCode 622. Design Circular Queue
原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...
- [LeetCode] Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [Swift]LeetCode641. 设计循环双端队列 | 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 ...
随机推荐
- SpringBoot 2.x 整合Lombok
Lombok的官方介绍 Project Lombok is a java library that automatically plugs into your editor and build too ...
- nginx location笔记
nginx location笔记= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码,因此请求为/static/20%/aa,可以被规则 ...
- 前端学习:学习笔记(HTML部分)
前端学习:学习笔记(HTML部分) HTML学习总结(图解) HTML简介 1.HTML是什么? 超文本标记语言 超文本:文字/图片/音频/视频.... 标记/标签 2.HTML的用途? 是用来编写静 ...
- 使用kibana给不同的用户创建不同的space
Elastic安全机制 在很多的情况下,出于安全的原因,我们需要对不同的Kibana用户分配不同的用户权限,这样使得他们之间不能互相访问彼此的资源,同 时他们也应该对不同的索引拥有不同的权限,比如读, ...
- Spring aop的一些小知识点总结
1 Spring的aop无法拦截静态方法 2 在 proxyTargetClass = false时 对于实现了接口的bean,则只有接口中的方法会被拦截: 对于没有实现任何接口的bean,publi ...
- Regex 常见语法
常用元字符 . 匹配除换行符以外的任意字符. \w 匹配字母或数字或下划线或汉字.\W 匹配任意不是字母,数字,下划线,汉字的字符. \s 匹配任意的空白符.\S 匹配任意不是空白符的字符.等价于 [ ...
- EIP Core2.0开源
EIP Core2 权限管理系统 (交流群:495070603,作者:1039318332) 开源地址: https://gitee.com/sunzewei/eipcore2 https://git ...
- Python关于多继承
大部分面向对象的编程语言(除了C++)都只支持单继承,而不支持多继承,为什么呢?因为多继承不仅增加编程复杂度,而且容易导致莫名其妙的错误. Python虽然语法上支持多继承,但是却不推荐使用多继承,而 ...
- JS的数组进行切片slice
代码 var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thoma ...
- 今天是JAVA诞生日
今天是JAVA诞生日,祝贺!!! 1995年5月23日,Sun公司在Sun world会议上正式发布Java和HotJava浏览器,Java诞生. https://baike.baidu.com/it ...