|
| 1 | +/* |
| 2 | +https://leetcode.com/problems/design-circular-deque/description/ |
| 3 | +
|
| 4 | +Design your implementation of the circular double-ended queue (deque). |
| 5 | +
|
| 6 | +Your implementation should support following operations: |
| 7 | +
|
| 8 | +MyCircularDeque(k): Constructor, set the size of the deque to be k. |
| 9 | +insertFront(): Adds an item at the front of Deque. Return true if the operation is successful. |
| 10 | +insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful. |
| 11 | +deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful. |
| 12 | +deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful. |
| 13 | +getFront(): Gets the front item from the Deque. If the deque is empty, return -1. |
| 14 | +getRear(): Gets the last item from Deque. If the deque is empty, return -1. |
| 15 | +isEmpty(): Checks whether Deque is empty or not. |
| 16 | +isFull(): Checks whether Deque is full or not. |
| 17 | + |
| 18 | +
|
| 19 | +Example: |
| 20 | +
|
| 21 | +MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3 |
| 22 | +circularDeque.insertLast(1); // return true |
| 23 | +circularDeque.insertLast(2); // return true |
| 24 | +circularDeque.insertFront(3); // return true |
| 25 | +circularDeque.insertFront(4); // return false, the queue is full |
| 26 | +circularDeque.getRear(); // return 2 |
| 27 | +circularDeque.isFull(); // return true |
| 28 | +circularDeque.deleteLast(); // return true |
| 29 | +circularDeque.insertFront(4); // return true |
| 30 | +circularDeque.getFront(); // return 4 |
| 31 | + |
| 32 | +
|
| 33 | +Note: |
| 34 | +
|
| 35 | +All values will be in the range of [0, 1000]. |
| 36 | +The number of operations will be in the range of [1, 1000]. |
| 37 | +Please do not use the built-in Deque library. |
| 38 | +*/ |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * Initialize your data structure here. Set the size of the deque to be k. |
| 43 | + * @param {number} k |
| 44 | + */ |
| 45 | +var MyCircularDeque = function(k) { |
| 46 | + this.queue = []; |
| 47 | + this.maxSize = k; |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | +* Adds an item at the front of Deque. Return true if the operation is successful. |
| 52 | +* @param {number} value |
| 53 | +* @return {boolean} |
| 54 | +*/ |
| 55 | +MyCircularDeque.prototype.insertFront = function(value) { |
| 56 | + if(this.isFull()) |
| 57 | + return false; |
| 58 | + |
| 59 | + this.queue.unshift(value); |
| 60 | + return true; |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | +* Adds an item at the rear of Deque. Return true if the operation is successful. |
| 65 | +* @param {number} value |
| 66 | +* @return {boolean} |
| 67 | +*/ |
| 68 | +MyCircularDeque.prototype.insertLast = function(value) { |
| 69 | + if(this.isFull()) |
| 70 | + return false; |
| 71 | + |
| 72 | + this.queue[this.queue.length] = value; |
| 73 | + return true; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | +* Deletes an item from the front of Deque. Return true if the operation is successful. |
| 78 | +* @return {boolean} |
| 79 | +*/ |
| 80 | +MyCircularDeque.prototype.deleteFront = function() { |
| 81 | + if(this.isEmpty()) |
| 82 | + return false; |
| 83 | + |
| 84 | + this.queue.shift(1); |
| 85 | + return true; |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | +* Deletes an item from the rear of Deque. Return true if the operation is successful. |
| 90 | +* @return {boolean} |
| 91 | +*/ |
| 92 | +MyCircularDeque.prototype.deleteLast = function() { |
| 93 | + if(this.isEmpty()) |
| 94 | + return false; |
| 95 | + |
| 96 | + this.queue.splice(this.queue.length - 1, 1); |
| 97 | + return true; |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | +* Get the front item from the deque. |
| 102 | +* @return {number} |
| 103 | +*/ |
| 104 | +MyCircularDeque.prototype.getFront = function() { |
| 105 | + if(this.isEmpty()) |
| 106 | + return -1; |
| 107 | + return this.queue[0]; |
| 108 | +}; |
| 109 | + |
| 110 | +/** |
| 111 | +* Get the last item from the deque. |
| 112 | +* @return {number} |
| 113 | +*/ |
| 114 | +MyCircularDeque.prototype.getRear = function() { |
| 115 | + if(this.isEmpty()) |
| 116 | + return -1; |
| 117 | + return this.queue[this.queue.length - 1]; |
| 118 | +}; |
| 119 | + |
| 120 | +/** |
| 121 | +* Checks whether the circular deque is empty or not. |
| 122 | +* @return {boolean} |
| 123 | +*/ |
| 124 | +MyCircularDeque.prototype.isEmpty = function() { |
| 125 | + return this.queue.length === 0; |
| 126 | +}; |
| 127 | + |
| 128 | +/** |
| 129 | +* Checks whether the circular deque is full or not. |
| 130 | +* @return {boolean} |
| 131 | +*/ |
| 132 | +MyCircularDeque.prototype.isFull = function() { |
| 133 | + return this.queue.length === this.maxSize; |
| 134 | +}; |
| 135 | + |
| 136 | +var main = function(){ |
| 137 | + const obj = new MyCircularDeque(3); |
| 138 | + console.log(obj.insertLast(1)); |
| 139 | + console.log(obj.insertLast(2)); |
| 140 | + console.log(obj.insertFront(3)); |
| 141 | + console.log(obj.insertFront(4)); |
| 142 | + console.log(obj.getRear()); |
| 143 | + console.log(obj.isFull()); |
| 144 | + console.log(obj.deleteLast()); |
| 145 | + console.log(obj.insertFront(4)); |
| 146 | + console.log(obj.getFront()); |
| 147 | +} |
| 148 | +main(); |
| 149 | +module.exports.main = main; |
0 commit comments