设计实现双端队列。
你的实现需要支持以下操作:
MyCircularDeque(k):构造函数,双端队列的大小为k。
insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。
insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。
deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。
isEmpty():检查双端队列是否为空。
isFull():检查双端队列是否满了。
示例:
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已经满了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 4

提示:
所有值的范围为 [1, 1000]
操作次数的范围为 [1, 1000]
请不要使用内置的双端队列库。

法一:常规

641. 设计循环双端队列()1 - 图1641. 设计循环双端队列()1 - 图2
641. 设计循环双端队列()1 - 图3641. 设计循环双端队列()1 - 图4
641. 设计循环双端队列()1 - 图5641. 设计循环双端队列()1 - 图6
641. 设计循环双端队列()1 - 图7641. 设计循环双端队列()1 - 图8
641. 设计循环双端队列()1 - 图9641. 设计循环双端队列()1 - 图10
641. 设计循环双端队列()1 - 图11641. 设计循环双端队列()1 - 图12
641. 设计循环双端队列()1 - 图13641. 设计循环双端队列()1 - 图14

  1. public class MyCircularDeque {
  2. // 1、不用设计成动态数组,使用静态数组即可
  3. // 2、设计 head 和 tail 指针变量
  4. // 3、head == tail 成立的时候表示队列为空
  5. // 4、tail + 1 == head
  6. private int capacity;
  7. private int[] arr;
  8. private int front;
  9. private int rear;
  10. /**
  11. * Initialize your data structure here. Set the size of the deque to be k.
  12. */
  13. public MyCircularDeque(int k) {
  14. capacity = k + 1;
  15. arr = new int[capacity];
  16. // 头部指向第 1 个存放元素的位置
  17. // 插入时,先减,再赋值
  18. // 删除时,索引 +1(注意取模)
  19. front = 0;
  20. // 尾部指向下一个插入元素的位置
  21. // 插入时,先赋值,再加
  22. // 删除时,索引 -1(注意取模)
  23. rear = 0;
  24. }
  25. /**
  26. * Adds an item at the front of Deque. Return true if the operation is successful.
  27. */
  28. public boolean insertFront(int value) {
  29. if (isFull()) {
  30. return false;
  31. }
  32. front = (front - 1 + capacity) % capacity;
  33. arr[front] = value;
  34. return true;
  35. }
  36. /**
  37. * Adds an item at the rear of Deque. Return true if the operation is successful.
  38. */
  39. public boolean insertLast(int value) {
  40. if (isFull()) {
  41. return false;
  42. }
  43. arr[rear] = value;
  44. rear = (rear + 1) % capacity;
  45. return true;
  46. }
  47. /**
  48. * Deletes an item from the front of Deque. Return true if the operation is successful.
  49. */
  50. public boolean deleteFront() {
  51. if (isEmpty()) {
  52. return false;
  53. }
  54. // front 被设计在数组的开头,所以是 +1
  55. front = (front + 1) % capacity;
  56. return true;
  57. }
  58. /**
  59. * Deletes an item from the rear of Deque. Return true if the operation is successful.
  60. */
  61. public boolean deleteLast() {
  62. if (isEmpty()) {
  63. return false;
  64. }
  65. // rear 被设计在数组的末尾,所以是 -1
  66. rear = (rear - 1 + capacity) % capacity;
  67. return true;
  68. }
  69. /**
  70. * Get the front item from the deque.
  71. */
  72. public int getFront() {
  73. if (isEmpty()) {
  74. return -1;
  75. }
  76. return arr[front];
  77. }
  78. /**
  79. * Get the last item from the deque.
  80. */
  81. public int getRear() {
  82. if (isEmpty()) {
  83. return -1;
  84. }
  85. // 当 rear 为 0 时防止数组越界
  86. return arr[(rear - 1 + capacity) % capacity];
  87. }
  88. /**
  89. * Checks whether the circular deque is empty or not.
  90. */
  91. public boolean isEmpty() {
  92. return front == rear;
  93. }
  94. /**
  95. * Checks whether the circular deque is full or not.
  96. */
  97. public boolean isFull() {
  98. // 注意:这个设计是非常经典的做法
  99. return (rear + 1) % capacity == front;
  100. }
  101. }