leetcode设计循环双端队列

这个题我也不太会,写在这里也是方便复习,若有大佬有好的理解方法,还请不吝赐教
在这里插入图片描述
首先,只要是循环队列,就需要设置两个指针分别指向队列的头和尾,但是由于是循环队列,本来我们单向队列判定队列为满的方法就无法使用了,头指针和尾指针同时指向同一个节点,无法判断是空还是满,这里我们有两种方法,一种是再引入一个变量来记录,队列中的数是否满,第二种就是将一个节点置为空,专门用来判断;这里我就使用的是第二种方法;

public class MyCircularDeque {

    private int capacity;
    private int[] arr;
    private int front;
    private int rear;

    /**
     * Initialize your data structure here. Set the size of the deque to be k.
     */
    public MyCircularDeque(int k) {
        capacity = k + 1;
        arr = new int[capacity];
		//开始由于没有值进入,所以头和尾都指向同一个节点

        // 头部指向第 1 个存放元素的位置
        // 插入时,先减,再赋值(这里可以理解为头只能往前走,顺时针)
        // 删除时,索引 +1(注意取模)
        front = 0;
        // 尾部指向下一个插入元素的位置
        // 插入时,先赋值,再加(尾只能往后走,)
        // 删除时,索引 -1(注意取模)
        rear = 0;
    }

    /**
     * Adds an item at the front of Deque. Return true if the operation is successful.
     */
    public boolean insertFront(int value) {
        if (isFull()) {
            return false;
        }
        front = (front - 1 + capacity) % capacity;
        arr[front] = value;
        return true;
    }

    /**
     * Adds an item at the rear of Deque. Return true if the operation is successful.
     */
    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        }
        arr[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
    }

    /**
     * Deletes an item from the front of Deque. Return true if the operation is successful.
     */
    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        }
        // front 被设计在数组的开头,所以是 +1
        front = (front + 1) % capacity;
        return true;
    }

    /**
     * Deletes an item from the rear of Deque. Return true if the operation is successful.
     */
    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        }
        // rear 被设计在数组的末尾,所以是 -1
        rear = (rear - 1 + capacity) % capacity;
        return true;
    }

    /**
     * Get the front item from the deque.
     */
    public int getFront() {
        if (isEmpty()) {
            return -1;
        }
        return arr[front];
    }

    /**
     * Get the last item from the deque.
     */
    public int getRear() {
        if (isEmpty()) {
            return -1;
        }
        // 当 rear 为 0 时防止数组越界
        return arr[(rear - 1 + capacity) % capacity];
    }

    /**
     * Checks whether the circular deque is empty or not.
     */
    public boolean isEmpty() {
        return front == rear;
    }

    /**
     * Checks whether the circular deque is full or not.
     */
    public boolean isFull() {
        // 注意:这个设计是非常经典的做法
        return (rear + 1) % capacity == front;
    }
}

2

class MyCircularDeque {
    int[] myqueue ;
    int front;//队头指针
    int rear;//队尾指针
    int size;//队列当前的大小
    int capacity;//队列的容量

    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
        this.myqueue = new int[k];
        this.front = 0;
        this.rear = 0;
        this.size = 0;
        this.capacity = k;
    }

    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public boolean insertFront(int value) {
        if(rear==front && size == capacity) return  false;//如果队列满,插入失败
        else {
            front = (front + capacity -1)% capacity;
            myqueue[front] = value;
            size++;
            return true;
        }
    }

    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public boolean insertLast(int value) {
        if(rear==front && size == capacity) return  false;//如果队列满,插入失败
        else {
            myqueue[rear] = value;
            rear = (rear+1+capacity)%capacity;
            size++;
            return true;
        }

    }

    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public boolean deleteFront() {
        if( rear == front && size == 0) return false;
        else {
            front = (front+1) % capacity;
            size--;
            return true;
        }

    }

    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public boolean deleteLast() {
        if( rear == front && size == 0) return false;
        else {
            rear = (rear - 1 + capacity) % capacity;
            size--;
            return true;
        }

    }

    /** Get the front item from the deque. */
    public int getFront() {
        if((rear == front) && size==0) return -1;
        else {
            int frontE = myqueue[front];
            //front = (front + 1) % capacity;
            //size--;
            return frontE;
        }
    }

    /** Get the last item from the deque. */
    public int getRear() {
        if((rear == front) && size==0) return -1;
        else {
            int rearE = myqueue[(rear-1+capacity)%capacity];
           // rear = (rear - 1 +capacity)%capacity;
            //size--;
            return rearE;
        }

    }

    /** Checks whether the circular deque is empty or not. */
    public boolean isEmpty() {
        return (rear == front) && size==0;
    }

    /** Checks whether the circular deque is full or not. */
    public boolean isFull() {
        return rear==front && size == capacity;

    }
}
LeetCode题目622的设计是一个“循环队列”(Circular Queue)问题。循环队列是一种特殊的线性数据结构,它类似于普通队列,但是它的两端是相连的,形成了一个环。这意味着元素可以在队列的一端入队,在另一端出队,而不会导致数据丢失。 在标准队列中,当队列满时,如果继续添加新元素,就会导致旧元素被覆盖。但在循环队列中,当你试图从队列尾部移除元素时,如果队列为空,实际上你会从头部开始移除,形成一个循环设计这样的数据结构通常会包含以下几个关键功能: 1. 初始化:创建一个新的空循环队列。 2. 入队(enqueue):将一个元素添加到队列尾部,并保持队列的循环特性。 3. 出队(dequeue):如果队列非空,删除并返回队头的元素;如果队列已满且无法再入队,处理特殊情况,如返回特殊值或抛出异常。 4. 查看队头元素(front):获取队头元素,但不删除。 5. 查看队尾元素(rear):获取队尾元素,同样不删除。 6. 判断队列是否为空(isEmpty):检查队列是否有元素。 7. 判断队列是否已满(isFull):检查队列是否达到其最大容量。 在实现时,可以考虑使用数组或者链表作为底层数据结构,同时维护两个指针,一个指向当前队头,一个指向下一个应该插入的位置,以便于操作。需要注意的是,由于循环特性,需要对这两个指针进行特别的处理,比如在判断队尾元素时需要考虑到队列大小限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值