原题链接在这里: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();
*/

类似Design Circular Queue.

最新文章

  1. js 制作MD5加密
  2. CodeForces#275--DIV 2--A
  3. The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决
  4. bootstrap学习总结-css组件(三)
  5. mysql show processlist 命令详解
  6. *windows下安装以及配置nginx
  7. 4.0 spring-注册解析的Bean
  8. WordPress 4.3 Beta 1 全新发布,改进了后台功能和用户体验
  9. 简单Spring和mybatis整合配置文件
  10. 编写原生JS的insertAfter函数
  11. JAVA的高并发编程
  12. 初探nginx
  13. .Net Core微服务系列--理论篇
  14. Identity Server 4 中文文档(v1.0.0)
  15. Python001-操作MSSQL(Microsoft sql server)基础示例(一)
  16. node-express-2-jade
  17. linux audit工具
  18. Windows下命令行怎样登录MySQL
  19. select option 选中 取消js
  20. [uboot] (第四章)uboot流程——uboot编译流程

热门文章

  1. 【操作系统之十五】iptables黑白名单、自定义链、网络防火墙、常用动作
  2. java,string和list,list和set相互转换
  3. Flink之state processor api原理
  4. MYSQL思维导图(转载)
  5. yarn安装node-sass失败问题
  6. CentOS7部署vsftpd服务
  7. Postman安装使用
  8. python 练习题:将列表中的大写字母转换成小写
  9. c#实现串口通信
  10. 2019 安易迅java面试笔试题 (含面试题解析)