原题链接在这里: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.

LeetCode 641. Design Circular Deque的更多相关文章

  1. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  2. LC 641. Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  3. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  4. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  5. LeetCode 622. Design Circular Queue

    原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...

  6. [LeetCode] Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  7. [Swift]LeetCode641. 设计循环双端队列 | Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  8. Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  9. Leetcode641.Design Circular Deque设计循环双端队列

    设计实现双端队列. 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k. insertFront():将一个元素添加到双端队列头部. 如果操作成功返回 tr ...

随机推荐

  1. docker环境下数据库的备份(postgresql, mysql)

    posgresql 备份/恢复 mysql 备份/恢复 补充 posgresql 备份/恢复 备份 DATE=`date +%Y%m%d-%H%M` BACK_DATA=xxapp-data-${DA ...

  2. C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)

    1.Abort当前线程,后续程序不会执行 class Program { public static Thread thread1; static void Main(string[] args) { ...

  3. JVM 的垃圾回收器详解

    Parallel Scavenge(Paraller):Parallel Scavenge和ParNew关注的点不一样:ParNew关注的是尽可能缩短暂停的时间,Parallel Scavenge关注 ...

  4. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  5. WebUploader 上传文件 错误总结

    近日做文件上传,粗心的问题和技术不精的问题导致了很多的bug,大部分时间都是在找自己写出来的bug,近日总结一下使用 WebUploader 开启分片上传的使用方法以及注意事项 1.上传过程中,后续上 ...

  6. 有趣的css图形实现

    css通过 border .border-radius .transform,实现各种图形. <!DOCTYPE html> <html lang="en"> ...

  7. .NET EF执行sql报数组超出了索引

    使用ef查询,写sql语句的 一般情况报数组超出了索引都认为是[i]里面的值超出了,但是执行sql报超出了索引,让人很蒙 在网上找了半天也没有结果,后来只能自己来解决了. 在异常里面能看到dbnull ...

  8. .net core使用jwt自动续期

    小弟不C才,最近看了下网上的jwt方案,于是自己写了一个简单的jwt方案和大家分享下,希望大家给点意见! 假如有一个读书网站,可以不用登陆就访问,当需要自己写文章的时候就必须登录,并且登录之后如果一段 ...

  9. BAT: Windows批处理更改当前工作路径

    最近项目上需要获取文件夹下所有文件信息,因为文件夹是在server上,所以想用批处理bat来获取该路径下所有文件信息,然后通过任务计划管理去每天自动运行bat去更新文件信息内容. 获取文件夹下所有文件 ...

  10. Linux之《荒岛余生》(三)内存篇

    原文:https://juejin.im/post/5c00aee06fb9a049be5d3641 小公司请求量小,但喜欢滥用内存,开一堆线程,大把大把往jvm塞对象,最终问题是内存溢出. 大公司并 ...