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