zoukankan      html  css  js  c++  java
  • 【LeetCode】622.设计循环队列

    题目

    链接

    image-20200701215435057

    分析

    道题说“循环”的意思是要求我们在数组里实现。

    使用链表的实现,创建结点和删除结点都是动态的,也就不存在需要循环利用的问题了。

    在数组里的操作,我们参考“动态数组”的实现来完成,主要是为了让每一步的操作复杂度都最低。只不过不要求我们实现动态扩容与缩容。

    要注意的地方有:

    1、定义循环变量 frontrear 。一直保持这个定义,到底是先赋值还是先移动指针就很容易想清楚了。

    front:指向队列头部第 1 个有效数据的位置;
    rear:指向队列尾部(即最后 1 个有效数据)的下一个位置,即下一个从队尾入队元素的位置。

    (说明:这个定义是依据“动态数组”的定义模仿而来。)

    2、为了避免“队列为空”和“队列为满”的判别条件冲突,我们有意浪费了一个位置。

    浪费一个位置是指:循环数组中任何时刻一定至少有一个位置不存放有效元素。

    • 判别队列为空的条件是:front == rear;
    • 判别队列为满的条件是:(rear + 1) % capacity == front;。可以这样理解,当 rear 循环到数组的前面,要从后面追上 front,还差一格的时候,判定队列为满。

    3、因为有循环的出现,要特别注意处理数组下标可能越界的情况。指针后移的时候,索引 + 1,并且要注意取模。

    参考代码

    public class MyCircularQueue {
    
        private int front;
        private int rear;
        private int capacity;
        private int[] arr;
    
        /**
         * Initialize your data structure here. Set the size of the queue to be k.
         */
        public MyCircularQueue(int k) {
            capacity = k + 1;
            arr = new int[capacity];
    
            // 在 front 出队,故设计在数组的头部,方便删除元素
            // 删除元素的时候,只索引 +1(注意取模)
    
    
            // 在 rear 入队,故设计在数组的尾部,方便插入元素
            // 插入元素的时候,先赋值,后索引 +1(注意取模)
            front = 0;
            rear = 0;
        }
    
        /**
         * Insert an element into the circular queue. Return true if the operation is successful.
         */
        public boolean enQueue(int value) {
            if (isFull()) {
                return false;
            }
            arr[rear] = value;
            rear = (rear + 1) % capacity;
            return true;
        }
    
        /**
         * Delete an element from the circular queue. Return true if the operation is successful.
         */
        public boolean deQueue() {
            if (isEmpty()) {
                return false;
            }
            front = (front + 1) % capacity;
            return true;
        }
    
        /**
         * Get the front item from the queue.
         */
        public int Front() {
            if (isEmpty()) {
                return -1;
            }
            return arr[front];
        }
    
        /**
         * Get the last item from the queue.
         */
        public int Rear() {
            if (isEmpty()) {
                return -1;
            }
            return arr[(rear - 1 + capacity) % capacity];
        }
    
        /**
         * Checks whether the circular queue is empty or not.
         */
        public boolean isEmpty() {
            return front == rear;
        }
    
        /**
         * Checks whether the circular queue is full or not.
         */
        public boolean isFull() {
            // 注意:这是这个经典设计的原因
            return (rear + 1) % capacity == front;
        }
    }
    
  • 相关阅读:
    构建布局良好的Windows程序
    新认知之WinForm窗体程序
    分组查询之牛刀小试!
    Azure虚拟机时间同步问题
    关闭同一网络内的windows主机
    关于jdbc和数据库连接池的关系(不是封装的关系)
    spring,springMVC的优点和区别
    从新向你学习javase(第一天)
    spring包
    事物
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13307998.html
Copyright © 2011-2022 走看看