当前位置: 移动技术网 > IT编程>脚本编程>Python > leetcode 622. design-circular-queue 设计循环队列 python3

leetcode 622. design-circular-queue 设计循环队列 python3

2020年07月12日  | 移动技术网IT编程  | 我要评论

时间:2020-7-11

题目地址:https://leetcode-cn.com/problems/design-circular-queue/

题目难度:Medium

题目描述:

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
 

示例:

MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
circularQueue.enQueue(1);  // 返回 true
circularQueue.enQueue(2);  // 返回 true
circularQueue.enQueue(3);  // 返回 true
circularQueue.enQueue(4);  // 返回 false,队列已满
circularQueue.Rear();  // 返回 3
circularQueue.isFull();  // 返回 true
circularQueue.deQueue();  // 返回 true
circularQueue.enQueue(4);  // 返回 true
circularQueue.Rear();  // 返回 4


思路1:列表

代码段1:通过

class MyCircularQueue:

    def __init__(self, k: int):
        """
        Initialize your data structure here. Set the size of the queue to be k.
        """
        self.size = 0
        self.length = k
        self.queue = [0] * k
        self.head, self.tail = -1, -1
        
    def enQueue(self, value: int) -> bool:
        """
        Insert an element into the circular queue. Return true if the operation is successful.
        """
        if self.size == 0 and self.head == -1:
            self.head += 1
        if self.size < self.length:
            self.tail += 1
            index = self.tail % self.length
            self.queue[index] = value
            self.size += 1
            return True 
        else:
            return False

    def deQueue(self) -> bool:
        """
        Delete an element from the circular queue. Return true if the operation is successful.
        """
        if self.size > 0:
            self.size -= 1
            self.queue[self.head % self.length] = None 
            self.head += 1
            return True
        else:
            return False

    def Front(self) -> int:
        """
        Get the front item from the queue.
        """
        if self.size != 0:
            index = self.head % self.length
            return self.queue[index]
        else:
            return -1
        
        
    def Rear(self) -> int:
        """
        Get the last item from the queue.
        """
        if self.size != 0:
            index = self.tail % self.length
            return self.queue[index]
        else:
            return -1
    def isEmpty(self) -> bool:
        """
        Checks whether the circular queue is empty or not.
        """
        if self.size == 0:
            return True
        else:
            return False

    def isFull(self) -> bool:
        """
        Checks whether the circular queue is full or not.
        """
        if self.size == self.length:
            return True
        else:
            return False

总结:

  1. 早上6点上了个户外瑜伽,中午12点上了个尊巴,上午调了一半的代码,将近一般的用例在早上没跑过,晚上花了一个小时改bug,就一个if错了,加了日志,很快就定位到了,果然写代码状态要好
  2. 看下官方的概念  很不错

设计数据结构的关键是如何设计 属性,好的设计属性数量更少。

属性数量少说明属性之间冗余更低。

属性冗余度越低,操作逻辑越简单,发生错误的可能性更低。

属性数量少,使用的空间也少,操作性能更高。

class MyCircularQueue:

    def __init__(self, k: int):
        """
        Initialize your data structure here. Set the size of the queue to be k.
        """
        self.queue = [0]*k
        self.headIndex = 0
        self.count = 0
        self.capacity = k

    def enQueue(self, value: int) -> bool:
        """
        Insert an element into the circular queue. Return true if the operation is successful.
        """
        if self.count == self.capacity:
            return False
        self.queue[(self.headIndex + self.count) % self.capacity] = value
        self.count += 1
        return True

    def deQueue(self) -> bool:
        """
        Delete an element from the circular queue. Return true if the operation is successful.
        """
        if self.count == 0:
            return False
        self.headIndex = (self.headIndex + 1) % self.capacity
        self.count -= 1
        return True

    def Front(self) -> int:
        """
        Get the front item from the queue.
        """
        if self.count == 0:
            return -1
        return self.queue[self.headIndex]

    def Rear(self) -> int:
        """
        Get the last item from the queue.
        """
        # empty queue
        if self.count == 0:
            return -1
        return self.queue[(self.headIndex + self.count - 1) % self.capacity]

    def isEmpty(self) -> bool:
        """
        Checks whether the circular queue is empty or not.
        """
        return self.count == 0

    def isFull(self) -> bool:
        """
        Checks whether the circular queue is full or not.
        """
        return self.count == self.capacity

后续优化:链表

本文地址:https://blog.csdn.net/isabloomingtree/article/details/107290573

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网