mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 15:01:58 +08:00
Modify the exception handling in Java and Python.
This commit is contained in:
@ -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]
|
||||
|
Reference in New Issue
Block a user