622. 设计循环队列 老师 感觉这个代码在LeetCode上测试不通过

来源:8-2 循环队列-代码实操

少言Syan

2019-04-11

我自己尝试过去调整你的代码 但是怎么样都是测试不通过 我自己有尝试调整 但是也没有提交成功

图片描述

写回答

6回答

nkliyc

2019-06-25

 isFull () {
        return this.front === this.rear && !!this.list[this.front]
    }

老师,!!this.list[this.front] 这个为什么必须要两个叹号,是这样的时候:

isFull () {
        return this.front === this.rear && this.list[this.front]
    }

通不过

0
0

nkliyc

2019-06-25

我的调整了一下,通过了

0
0

nkliyc

2019-06-25

class MyCircularQueue{
    constructor (k) {
        // 用来保存数据长度为K的数据结构
        this.list = Array(k)
        // 队首指针
        this.front = 0
        // 队尾指针
        this.rear = 0
        // 队列长度
        this.max = k
    }
    enQueue (value) {
        if(this.isFull()) {
            return false
        } else {
            this.list[this.rear] = value
            this.rear = (this.rear + 1) % this.max
            return true
        }
    }
    deQueue () {
        if(this.isEmpty()) {
            return false
        } else {
            this.list[this.front] = null
            this.front = (this.front + 1) % this.max
            return true
        }

    }
    Front () {
        if(this.isEmpty()) {
            return -1
        } else {
            return this.list[this.front]
        }
    }
    Rear () {
       if(this.isEmpty()) {
            return -1
        } else {
            let rear = (this.max + this.rear - 1) % this.max
            return this.list[rear]
        }
    }
    isEmpty () {
        return this.front === this.rear && !this.list[this.front]
    }
    isFull () {
        return this.front === this.rear && !!this.list[this.front]
    }

};

 

//img.mukewang.com/szimg/5d10f8a9000155b619311045.jpg

0
0

ngNode

2019-05-14

/**
 * Initialize your data structure here. Set the size of the queue to be k.
 * @param {number} k
 */
var MyCircularQueue = function(k) {
    this.list = new Array(k);
    this.front = 0;
    this.rear = 0;
    this.max = k;
};

/**
 * Insert an element into the circular queue. Return true if the operation is successful. 
 * @param {number} value
 * @return {boolean}
 */
MyCircularQueue.prototype.enQueue = function(value) {
    if(this.isFull()) {
        return false;
    } else {
        this.list[this.rear] = value;
        this.rear = (this.rear + 1) % this.max;
        return true;
    }
};

/**
 * Delete an element from the circular queue. Return true if the operation is successful.
 * @return {boolean}
 */
MyCircularQueue.prototype.deQueue = function() {
    this.list[this.front] = '';
    this.front = (this.front + 1) % this.max;
    return true;
};

/**
 * Get the front item from the queue.
 * @return {number}
 */
MyCircularQueue.prototype.Front = function() {
   return this.list[this.front] || -1;
};

/**
 * Get the last item from the queue.
 * @return {number}
 */
MyCircularQueue.prototype.Rear = function() {
    let rear = this.rear-1 < 0 ? this.max-1 : this.rear-1;
    return this.list[rear] || -1;
};

/**
 * Checks whether the circular queue is empty or not.
 * @return {boolean}
 */
MyCircularQueue.prototype.isEmpty = function() {
    return this.front === this.rear && !this.list[this.front];
};

/**
 * Checks whether the circular queue is full or not.
 * @return {boolean}
 */
MyCircularQueue.prototype.isFull = function() {
    return this.front === this.rear && !!this.list[this.front];
};

/** 
 * Your MyCircularQueue object will be instantiated and called as such:
 * var obj = new MyCircularQueue(k)
 * var param_1 = obj.enQueue(value)
 * var param_2 = obj.deQueue()
 * var param_3 = obj.Front()
 * var param_4 = obj.Rear()
 * var param_5 = obj.isEmpty()
 * var param_6 = obj.isFull()
 */

输入为:
["MyCircularQueue","enQueue","Rear","Rear","deQueue","enQueue","Rear","deQueue","Front","deQueue","deQueue","deQueue"]
[[6],[6],[],[],[],[5],[],[],[],[],[],[]]

时一样失败;

0
1
ngNode
可以了,少了判断0 和 []的情况
2019-05-14
共1条回复

东方既白233

2019-05-11

/**
 * Initialize your data structure here. Set the size of the queue to be k.
 * @param {number} k
 */
var MyCircularQueue = function(k) {
this.list = new Array(k);
this.front = 0;
this.rear = 0;
this.max = k;
};

/**
 * Insert an element into the circular queue. Return true if the operation is successful.
 * @param {number} value
 * @return {boolean}
 */
MyCircularQueue.prototype.enQueue = function(value) {
if (this.isFull()) {
return false;
}
this.list[this.rear] = value;
this.rear = (this.rear + 1) % this.max;
return true;
};

/**
 * Delete an element from the circular queue. Return true if the operation is successful.
 * @return {boolean}
 */
MyCircularQueue.prototype.deQueue = function() {
if(this.isEmpty()) return false;
this.list[this.front] = "";
this.front = (this.front + 1) % this.max;
return true;
};

/**
 * Get the front item from the queue.
 * @return {number}
 */
MyCircularQueue.prototype.Front = function() {
if(this.isEmpty()) return -1;
return this.list[this.front];
};

/**
 * Get the last item from the queue.
 * @return {number}
 */
MyCircularQueue.prototype.Rear = function() {
if(this.isEmpty()) return -1;
let rear = this.rear - 1;
return this.list[rear < 0 ? this.max - 1 : rear];
};

/**
 * Checks whether the circular queue is empty or not.
 * @return {boolean}
 */
MyCircularQueue.prototype.isEmpty = function() {
return this.front === this.rear && !this.list[this.front];
};

/**
 * Checks whether the circular queue is full or not.
 * @return {boolean}
 */
MyCircularQueue.prototype.isFull = function() {
return this.front === this.rear && !!this.list[this.front]
};

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * var obj = new MyCircularQueue(k)
 * var param_1 = obj.enQueue(value)
 * var param_2 = obj.deQueue()
 * var param_3 = obj.Front()
 * var param_4 = obj.Rear()
 * var param_5 = obj.isEmpty()
 * var param_6 = obj.isFull()
 */

这个代码经过修改已经通过leetcode测试的,建议一切以leetcode为主,慕课网这个课只能说作为参考,回答比较应付式

0
0

快乐动起来呀

2019-04-13

可以把你调整的代码提交的慕课的git issue吗

0
0

JavaScript版 数据结构与算法

填补前端同学的算法短板,掌握面试中最常见的算法与数据结构

2467 学习 · 395 问题

查看课程