原题链接在这里: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:
1 class MyCircularDeque { 2 int [] arr; 3 int start; 4 int end; 5 int len; 6 int k; 7 8 /** Initialize your data structure here. Set the size of the deque to be k. */ 9 public MyCircularDeque(int k) { 10 arr = new int[k]; 11 start = -1; 12 end = -1; 13 len = 0; 14 this.k = k; 15 } 16 17 /** Adds an item at the front of Deque. Return true if the operation is successful. */ 18 public boolean insertFront(int value) { 19 if(isFull()){ 20 return false; 21 } 22 23 if(start == -1){ 24 start = 0; 25 }else{ 26 start = (start - 1 + k) % k; 27 } 28 29 arr[start] = value; 30 if(end == -1){ 31 end = start; 32 } 33 34 len++; 35 return true; 36 } 37 38 /** Adds an item at the rear of Deque. Return true if the operation is successful. */ 39 public boolean insertLast(int value) { 40 if(isFull()){ 41 return false; 42 } 43 44 end = (end + 1) % k; 45 arr[end] = value; 46 if(start == -1){ 47 start = end; 48 } 49 50 len++; 51 return true; 52 } 53 54 /** Deletes an item from the front of Deque. Return true if the operation is successful. */ 55 public boolean deleteFront() { 56 if(isEmpty()){ 57 return false; 58 } 59 60 start = (start + 1) % k; 61 len--; 62 return true; 63 } 64 65 /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ 66 public boolean deleteLast() { 67 if(isEmpty()){ 68 return false; 69 } 70 71 end = (end - 1 + k) % k; 72 len--; 73 return true; 74 } 75 76 /** Get the front item from the deque. */ 77 public int getFront() { 78 return isEmpty() ? -1 : arr[start]; 79 } 80 81 /** Get the last item from the deque. */ 82 public int getRear() { 83 return isEmpty() ? -1 : arr[end]; 84 } 85 86 /** Checks whether the circular deque is empty or not. */ 87 public boolean isEmpty() { 88 return len == 0; 89 } 90 91 /** Checks whether the circular deque is full or not. */ 92 public boolean isFull() { 93 return len == k; 94 } 95 } 96 97 /** 98 * Your MyCircularDeque object will be instantiated and called as such: 99 * MyCircularDeque obj = new MyCircularDeque(k); 100 * boolean param_1 = obj.insertFront(value); 101 * boolean param_2 = obj.insertLast(value); 102 * boolean param_3 = obj.deleteFront(); 103 * boolean param_4 = obj.deleteLast(); 104 * int param_5 = obj.getFront(); 105 * int param_6 = obj.getRear(); 106 * boolean param_7 = obj.isEmpty(); 107 * boolean param_8 = obj.isFull(); 108 */