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;
        }
    }
    
  • 相关阅读:
    Python实现杨辉三角
    第8.30节 重写Python __setattr__方法实现属性修改捕获
    转:Cookie详解
    第8.29节 使用MethodType将Python __setattr__定义的实例方法与实例绑定
    第8.28节 Python中使用__setattr__定义实例变量和实例方法
    第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
    第8.26节 重写Python类中的__getattribute__方法实现实例属性访问捕获
    关于open函数文件打开模式的有意思的一个现象
    第8.25节 Python风格的__getattribute__属性访问方法语法释义及使用
    转:关于Python中的lambda,这篇阅读量10万+的文章可能是你见过的最完整的讲解
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13307998.html
Copyright © 2011-2022 走看看