LeetCode: Design Circular Queue Solution
1/**2 * @param {number} k3 */4var MyCircularQueue = function (k) {5 this.arr = []6 this.maxSize = k7}89/**10 * @param {number} value11 * @return {boolean}12 */13MyCircularQueue.prototype.enQueue = function (value) {14 if (this.isFull()) return false15 this.arr.push(value)16 return true17}1819/**20 * @return {boolean}21 */22MyCircularQueue.prototype.deQueue = function () {23 if (this.isEmpty()) return false24 this.arr.shift()25 return true26}2728/**29 * @return {number}30 */31MyCircularQueue.prototype.Front = function () {32 return this.isEmpty() ? -1 : this.arr[0]33}3435/**36 * @return {number}37 */38MyCircularQueue.prototype.Rear = function () {39 return this.isEmpty() ? -1 : this.arr.slice(-1)[0]40}4142/**43 * @return {boolean}44 */45MyCircularQueue.prototype.isEmpty = function () {46 return this.arr.length === 047}4849/**50 * @return {boolean}51 */52MyCircularQueue.prototype.isFull = function () {53 return this.arr.length === this.maxSize54}5556/**57 * Your MyCircularQueue object will be instantiated and called as such:58 * var obj = new MyCircularQueue(k)59 * var param_1 = obj.enQueue(value)60 * var param_2 = obj.deQueue()61 * var param_3 = obj.Front()62 * var param_4 = obj.Rear()63 * var param_5 = obj.isEmpty()64 * var param_6 = obj.isFull()65 */
Comments
Loading comments...
Tags
leetcode
queue
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.