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 0.
end index pointing to queue tail, initialized as k - 1.
When insertFront, if start == -1, assign start as 0, else start = (start - 1 + k) % k. Assign new value to arr[start].
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 public MyCircularDeque(int k) { 8 arr = new int[k]; 9 start = 0; 10 end = k - 1 ; 11 len = 0; 12 this.k = k; 13 } 14 15 public boolean insertFront(int value) { 16 if(isFull()){ 17 return false; 18 } 19 20 start = (start - 1 + k) % k; 21 arr[start] = value; 22 len++; 23 24 return true; 25 } 26 27 public boolean insertLast(int value) { 28 if(isFull()){ 29 return false; 30 } 31 32 end = (end + 1) % k; 33 arr[end] = value; 34 len++; 35 return true; 36 } 37 38 public boolean deleteFront() { 39 if(isEmpty()){ 40 return false; 41 } 42 43 start = (start + 1) % k; 44 len--; 45 return true; 46 } 47 48 public boolean deleteLast() { 49 if(isEmpty()){ 50 return false; 51 } 52 53 end = (end - 1 + k) % k; 54 len--; 55 return true; 56 } 57 58 public int getFront() { 59 return isEmpty() ? -1 : arr[start]; 60 } 61 62 public int getRear() { 63 return isEmpty() ? -1 : arr[end]; 64 } 65 66 public boolean isEmpty() { 67 return len == 0; 68 } 69 70 public boolean isFull() { 71 return len == k; 72 } 73 } 74 75 /** 76 * Your MyCircularDeque object will be instantiated and called as such: 77 * MyCircularDeque obj = new MyCircularDeque(k); 78 * boolean param_1 = obj.insertFront(value); 79 * boolean param_2 = obj.insertLast(value); 80 * boolean param_3 = obj.deleteFront(); 81 * boolean param_4 = obj.deleteLast(); 82 * int param_5 = obj.getFront(); 83 * int param_6 = obj.getRear(); 84 * boolean param_7 = obj.isEmpty(); 85 * boolean param_8 = obj.isFull(); 86 */