|
| 1 | +/** |
| 2 | + * 641. Design Circular Deque |
| 3 | + * https://leetcode.com/problems/design-circular-deque/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * Design your implementation of the circular double-ended queue (deque). |
| 7 | + * |
| 8 | + * Implement the MyCircularDeque class: |
| 9 | + * - MyCircularDeque(int k) Initializes the deque with a maximum size of k. |
| 10 | + * - boolean insertFront() Adds an item at the front of Deque. Returns true if the operation |
| 11 | + * is successful, or false otherwise. |
| 12 | + * - boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation |
| 13 | + * is successful, or false otherwise. |
| 14 | + * - boolean deleteFront() Deletes an item from the front of Deque. Returns true if the |
| 15 | + * operation is successful, or false otherwise. |
| 16 | + * - boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation |
| 17 | + * is successful, or false otherwise. |
| 18 | + * - int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty. |
| 19 | + * - int getRear() Returns the last item from Deque. Returns -1 if the deque is empty. |
| 20 | + * - boolean isEmpty() Returns true if the deque is empty, or false otherwise. |
| 21 | + * - boolean isFull() Returns true if the deque is full, or false otherwise. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @param {number} k |
| 26 | + */ |
| 27 | +var MyCircularDeque = function(k) { |
| 28 | + this.queue = new Array(k); |
| 29 | + this.size = k; |
| 30 | + this.front = 0; |
| 31 | + this.rear = -1; |
| 32 | + this.count = 0; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * @param {number} value |
| 37 | + * @return {boolean} |
| 38 | + */ |
| 39 | +MyCircularDeque.prototype.insertFront = function(value) { |
| 40 | + if (this.isFull()) return false; |
| 41 | + this.front = (this.front - 1 + this.size) % this.size; |
| 42 | + this.queue[this.front] = value; |
| 43 | + this.count++; |
| 44 | + if (this.count === 1) this.rear = this.front; |
| 45 | + return true; |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {number} value |
| 50 | + * @return {boolean} |
| 51 | + */ |
| 52 | +MyCircularDeque.prototype.insertLast = function(value) { |
| 53 | + if (this.isFull()) return false; |
| 54 | + this.rear = (this.rear + 1) % this.size; |
| 55 | + this.queue[this.rear] = value; |
| 56 | + this.count++; |
| 57 | + return true; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * @return {boolean} |
| 62 | + */ |
| 63 | +MyCircularDeque.prototype.deleteFront = function() { |
| 64 | + if (this.isEmpty()) return false; |
| 65 | + this.front = (this.front + 1) % this.size; |
| 66 | + this.count--; |
| 67 | + return true; |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * @return {boolean} |
| 72 | + */ |
| 73 | +MyCircularDeque.prototype.deleteLast = function() { |
| 74 | + if (this.isEmpty()) return false; |
| 75 | + this.rear = (this.rear - 1 + this.size) % this.size; |
| 76 | + this.count--; |
| 77 | + return true; |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + * @return {number} |
| 82 | + */ |
| 83 | +MyCircularDeque.prototype.getFront = function() { |
| 84 | + return this.isEmpty() ? -1 : this.queue[this.front]; |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * @return {number} |
| 89 | + */ |
| 90 | +MyCircularDeque.prototype.getRear = function() { |
| 91 | + return this.isEmpty() ? -1 : this.queue[this.rear]; |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * @return {boolean} |
| 96 | + */ |
| 97 | +MyCircularDeque.prototype.isEmpty = function() { |
| 98 | + return this.count === 0; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * @return {boolean} |
| 103 | + */ |
| 104 | +MyCircularDeque.prototype.isFull = function() { |
| 105 | + return this.count === this.size; |
| 106 | +}; |
0 commit comments