Modify the exception handling in Java and Python.

This commit is contained in:
krahets
2023-04-23 03:41:39 +08:00
parent 7e59e2c7fb
commit 3590262c7e
14 changed files with 35 additions and 28 deletions

View File

@ -72,12 +72,14 @@ class ArrayDeque:
def peek_first(self) -> int:
"""访问队首元素"""
assert not self.is_empty(), "双向队列为空"
if self.is_empty():
raise IndexError("双向队列为空")
return self.__nums[self.__front]
def peek_last(self) -> int:
"""访问队尾元素"""
assert not self.is_empty(), "双向队列为空"
if self.is_empty():
raise IndexError("双向队列为空")
# 计算尾元素索引
last = self.index(self.__front + self.__size - 1)
return self.__nums[last]