mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-08 07:15:59 +08:00
Modify the exception handling in Java and Python.
This commit is contained in:
@ -28,9 +28,10 @@ class ArrayQueue:
|
||||
|
||||
def push(self, num: int) -> None:
|
||||
"""入队"""
|
||||
assert self.__size < self.capacity(), "队列已满"
|
||||
if self.__size == self.capacity():
|
||||
raise IndexError("队列已满")
|
||||
# 计算尾指针,指向队尾索引 + 1
|
||||
# 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
# 通过取余操作,实现 rear 越过数组尾部后回到头部F
|
||||
rear: int = (self.__front + self.__size) % self.capacity()
|
||||
# 将 num 添加至队尾
|
||||
self.__nums[rear] = num
|
||||
@ -46,7 +47,8 @@ class ArrayQueue:
|
||||
|
||||
def peek(self) -> int:
|
||||
"""访问队首元素"""
|
||||
assert not self.is_empty(), "队列为空"
|
||||
if self.is_empty():
|
||||
raise IndexError("队列为空")
|
||||
return self.__nums[self.__front]
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
|
Reference in New Issue
Block a user