【路飞】算法与数据结构-设计前中后队列

128 阅读2分钟

我正在参与掘金新人创作活动,一起开启写作之路。

算法与数据结构-设计前中后队列

LeetCode:地址

题目要求

请你设计一个队列,支持在前,中,后三个位置的 push 和 pop 操作。

请你完成 FrontMiddleBack 类:

FrontMiddleBack() 初始化队列。 void pushFront(int val) 将 val 添加到队列的 最前面 。 void pushMiddle(int val) 将 val 添加到队列的 正中间 。 void pushBack(int val) 将 val 添加到队里的 最后面 。 int popFront() 将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 int popMiddle() 将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 int popBack() 将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 请注意当有 两个 中间位置的时候,选择靠前面的位置进行操作。比方说:

将 6 添加到 [1, 2, 3, 4, 5] 的中间位置,结果数组为 [1, 2, 6, 3, 4, 5] 。 从 [1, 2, 3, 4, 5, 6] 的中间位置弹出元素,返回 3 ,数组变为 [1, 2, 4, 5, 6] 。

示例 1:

输入:
["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"]
[[], [1], [2], [3], [4], [], [], [], [], []]
输出:
[null, null, null, null, null, 1, 3, 4, 2, -1]

解释:
FrontMiddleBackQueue q = new FrontMiddleBackQueue();
q.pushFront(1);   // [1]
q.pushBack(2);    // [1, 2]
q.pushMiddle(3);  // [1, 3, 2]
q.pushMiddle(4);  // [1, 4, 3, 2]
q.popFront();     // 返回 1 -> [4, 3, 2]
q.popMiddle();    // 返回 3 -> [4, 2]
q.popMiddle();    // 返回 4 -> [2]
q.popBack();      // 返回 2 -> []
q.popFront();     // 返回 -1 -> [] (队列为空)

提示:

1 <= val <= 109 最多调用 1000 次 pushFront, pushMiddle, pushBack, popFront, popMiddle 和 popBack 。

思路

主要题目是在链表中出的,所以就用链表来模拟 基本都是试出来的 主要注意空链表的时候,其次是中间节点的push 和 pop 的时机,他们获取的位置是不一样的 middle 获取的时候要去的 mid 是后置节点,pop 的时候获取的是前置节点,所以用快慢指针获取的时候判断条件会发生变更

代码
var FrontMiddleBackQueue = function() {
    this.head = this.tail = null
};

/** 
 * @param {number} val
 * @return {void}
 */
FrontMiddleBackQueue.prototype.pushFront = function(val) {
    if(!this.head){
        this.head = this.tail = new ListNode(val)
        this.head.next = this.tail
        this.tail.next = null
    }else{
        let temp = new ListNode(val)
        temp.next = this.head
        this.head = temp
    }
};

/** 
 * @param {number} val
 * @return {void}
 */
FrontMiddleBackQueue.prototype.pushMiddle = function(val) {
    if(!this.head){
        this.head = this.tail = new ListNode(val)
        this.head.next = this.tail
        this.tail.next = null

    }else{
        let emptyNode = prev = new ListNode()
        emptyNode.next = this.head
        let slow = fast = this.head
        while(fast && fast.next){
            prev = prev.next
            slow = slow.next
            fast = fast.next.next
        }
        let temp = new ListNode(val)
        prev.next = temp
        temp.next = slow
        this.head = emptyNode.next
    }
    
};

/** 
 * @param {number} val
 * @return {void}
 */
FrontMiddleBackQueue.prototype.pushBack = function(val) {
    if(!this.head){
        this.head = this.tail = new ListNode(val)
        this.head.next = this.tail
        this.tail.next = null
    }else{
        this.tail.next = new ListNode(val)
        this.tail = this.tail.next
    }
};

/**
 * @return {number}
 */
FrontMiddleBackQueue.prototype.popFront = function() {
    if(!this.head) return -1
    if(!this.head.next){
        let temp = this.head.val
        this.head = this.tail = null
        return temp
    }
    const temp = this.head.val
    this.head = this.head.next
    return temp
};

/**
 * @return {number}
 */
FrontMiddleBackQueue.prototype.popMiddle = function() {
    if(!this.head) return -1
    if(!this.head.next){
        let temp = this.head.val
        this.head = this.tail = null
        return temp
    }
    let emptyNode = prev = new ListNode()
    emptyNode.next = this.head
    let slow = fast = this.head
    while(fast.next && fast.next.next){
        prev = prev.next
        slow = slow.next
        fast = fast.next.next
    }
    prev.next = slow.next
    this.head = emptyNode.next
    return slow.val
};

/**
 * @return {number}
 */
FrontMiddleBackQueue.prototype.popBack = function() {
    if(!this.head) return -1
    if(!this.head.next){
        let temp = this.head.val
        this.head = this.tail = null
        return temp
    }
    const res = this.tail.val
    let cur = this.head

    while(cur && cur.next){
        if(cur.next === this.tail){
            this.tail = cur
            this.tail.next = null
        }
        cur = cur.next
    }
    return res
};

/**
 * Your FrontMiddleBackQueue object will be instantiated and called as such:
 * var obj = new FrontMiddleBackQueue()
 * obj.pushFront(val)
 * obj.pushMiddle(val)
 * obj.pushBack(val)
 * var param_4 = obj.popFront()
 * var param_5 = obj.popMiddle()
 * var param_6 = obj.popBack()
 */