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 ...
随机推荐
- vue+elementui搭建后台管理界面(1登录)
1 node环境安装 从 node官网下载安装包 2 vue-cli npm install vue-cli -g 3 新建项目 vue init webpack vue-project 可保持默认, ...
- 使用Windows的Linux子系统搭建嵌入式开发环境
亲,都9102年了,还在用VMware跑嵌入式交叉编译链吗? 北京时间2019年6月13日,Windows 10发布预览版本18917.版本的主要功能是Linux子系统(windows sub ...
- CentOS7 GitLab 安装
1.安装依赖 $ yum -y install policycoreutils openssh-server openssh-clients postfix $ yum install policyc ...
- HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
window.close(); System.Diagnostics.Process[] myProcesses; myProcesses = System.Diagnostics ...
- APP 链接ROS时出现pymongo.errors.ServerSelectionTimeoutError: localhost:27017 错误
ROS版本上kinetic ,APP是官网开源的make a map,当app链接ROS进行建图时,会出现报错:pymongo.errors.ServerSelectionTimeoutError: ...
- linux php composer安装和使用教程
linux php composer安装和使用教程建议在linux下 下载后 然后再下载到本地 win上最好别用composer下载速度超级慢 或者根本下不动 项目依赖包 ...
- day06——小数据池、深浅拷贝、集合
day06 小数据池 小数据池--缓存机制(驻留机制),只是一种规格,不会实际的开辟一个空间 == 判断两边内容是否相等 ***** # a = 10 # b = 10 # print(a == b) ...
- ASP.NET Core 3.0 解决无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称错误
写在前面 在 ASP.NET Core 的项目中 使用 CodeFirst 的模式,进行初始化迁移时.出现如图所示的问题: 在度娘哪里查了半天之后,才从这个帖子里找到了答案.传送门 分析原因 ASP. ...
- sonarqube+sonar runner分析C#代码
最近研究一个代码覆盖率和代码分析工具.遇到一些比较坑的问题,现在分享给大家. 1.Sonar介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量.通过插件机制,Sonar 可 ...
- php 获取文件下的所有文件。php 获取文件下的所有子文件。php 递归获取文件下的所有文件。封装好的方法
//php 获取文件下的所有文件.php 获取文件下的所有子文件.php 递归获取文件下的所有文件.直接上封装好的php代码 <?php //文件路径 $dir = dirname(__FILE ...