diff --git a/codes/java/chapter_heap/my_heap.java b/codes/java/chapter_heap/my_heap.java index 83cde00bf..03ff3c320 100644 --- a/codes/java/chapter_heap/my_heap.java +++ b/codes/java/chapter_heap/my_heap.java @@ -90,7 +90,7 @@ class MaxHeap { public int pop() { // 判空处理 if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); // 交换根节点与最右叶节点(即交换首元素与尾元素) swap(0, size() - 1); // 删除节点 diff --git a/codes/java/chapter_stack_and_queue/array_deque.java b/codes/java/chapter_stack_and_queue/array_deque.java index af8bced22..a2621e845 100644 --- a/codes/java/chapter_stack_and_queue/array_deque.java +++ b/codes/java/chapter_stack_and_queue/array_deque.java @@ -89,14 +89,14 @@ class ArrayDeque { /* 访问队首元素 */ public int peekFirst() { if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return nums[front]; } /* 访问队尾元素 */ public int peekLast() { if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); // 计算尾元素索引 int last = index(front + queSize - 1); return nums[last]; diff --git a/codes/java/chapter_stack_and_queue/array_queue.java b/codes/java/chapter_stack_and_queue/array_queue.java index 254973362..aa9922ebd 100644 --- a/codes/java/chapter_stack_and_queue/array_queue.java +++ b/codes/java/chapter_stack_and_queue/array_queue.java @@ -60,7 +60,7 @@ class ArrayQueue { /* 访问队首元素 */ public int peek() { if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return nums[front]; } diff --git a/codes/java/chapter_stack_and_queue/array_stack.java b/codes/java/chapter_stack_and_queue/array_stack.java index 07557b3ea..6369dcedf 100644 --- a/codes/java/chapter_stack_and_queue/array_stack.java +++ b/codes/java/chapter_stack_and_queue/array_stack.java @@ -35,14 +35,14 @@ class ArrayStack { /* 出栈 */ public int pop() { if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return stack.remove(size() - 1); } /* 访问栈顶元素 */ public int peek() { if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return stack.get(size() - 1); } diff --git a/codes/java/chapter_stack_and_queue/linkedlist_queue.java b/codes/java/chapter_stack_and_queue/linkedlist_queue.java index 5b0ee8438..87682ccb1 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_queue.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_queue.java @@ -56,7 +56,7 @@ class LinkedListQueue { /* 访问队首元素 */ public int peek() { if (size() == 0) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return front.val; } diff --git a/codes/java/chapter_stack_and_queue/linkedlist_stack.java b/codes/java/chapter_stack_and_queue/linkedlist_stack.java index 1022fb328..0e0ec73c9 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_stack.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_stack.java @@ -47,7 +47,7 @@ class LinkedListStack { /* 访问栈顶元素 */ public int peek() { if (size() == 0) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return stackPeek.val; } diff --git a/codes/python/chapter_array_and_linkedlist/my_list.py b/codes/python/chapter_array_and_linkedlist/my_list.py index 287ec0dee..fda11760c 100644 --- a/codes/python/chapter_array_and_linkedlist/my_list.py +++ b/codes/python/chapter_array_and_linkedlist/my_list.py @@ -26,12 +26,14 @@ class MyList: def get(self, index: int) -> int: """访问元素""" # 索引如果越界则抛出异常,下同 - assert index >= 0 and index < self.__size, "索引越界" + if index < 0 or index >= self.__size: + raise IndexError("索引越界") return self.__nums[index] def set(self, num: int, index: int) -> None: """更新元素""" - assert index >= 0 and index < self.__size, "索引越界" + if index < 0 or index >= self.__size: + raise IndexError("索引越界") self.__nums[index] = num def add(self, num: int) -> None: @@ -44,7 +46,8 @@ class MyList: def insert(self, num: int, index: int) -> None: """中间插入元素""" - assert index >= 0 and index < self.__size, "索引越界" + if index < 0 or index >= self.__size: + raise IndexError("索引越界") # 元素数量超出容量时,触发扩容机制 if self.__size == self.capacity(): self.extend_capacity() @@ -57,7 +60,8 @@ class MyList: def remove(self, index: int) -> int: """删除元素""" - assert index >= 0 and index < self.__size, "索引越界" + if index < 0 or index >= self.__size: + raise IndexError("索引越界") num = self.__nums[index] # 索引 i 之后的元素都向前移动一位 for j in range(index, self.__size - 1): @@ -114,5 +118,5 @@ if __name__ == "__main__": # 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 my_list.add(i) print( - "扩容后的列表 {my_list.to_array()} ,容量 = {my_list.capacity()} ,长度 = {my_list.size()}" + f"扩容后的列表 {my_list.to_array()} ,容量 = {my_list.capacity()} ,长度 = {my_list.size()}" ) diff --git a/codes/python/chapter_graph/graph_adjacency_list.py b/codes/python/chapter_graph/graph_adjacency_list.py index 39806cccb..1172b4625 100644 --- a/codes/python/chapter_graph/graph_adjacency_list.py +++ b/codes/python/chapter_graph/graph_adjacency_list.py @@ -30,7 +30,7 @@ class GraphAdjList: def add_edge(self, vet1: Vertex, vet2: Vertex) -> None: """添加边""" if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2: - raise ValueError + raise ValueError() # 添加边 vet1 - vet2 self.adj_list[vet1].append(vet2) self.adj_list[vet2].append(vet1) @@ -38,7 +38,7 @@ class GraphAdjList: def remove_edge(self, vet1: Vertex, vet2: Vertex) -> None: """删除边""" if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2: - raise ValueError + raise ValueError() # 删除边 vet1 - vet2 self.adj_list[vet1].remove(vet2) self.adj_list[vet2].remove(vet1) @@ -53,7 +53,7 @@ class GraphAdjList: def remove_vertex(self, vet: Vertex) -> None: """删除顶点""" if vet not in self.adj_list: - raise ValueError + raise ValueError() # 在邻接表中删除顶点 vet 对应的链表 self.adj_list.pop(vet) # 遍历其他顶点的链表,删除所有包含 vet 的边 diff --git a/codes/python/chapter_heap/my_heap.py b/codes/python/chapter_heap/my_heap.py index c772f8800..08ce0a6a2 100644 --- a/codes/python/chapter_heap/my_heap.py +++ b/codes/python/chapter_heap/my_heap.py @@ -73,7 +73,8 @@ class MaxHeap: def pop(self) -> int: """元素出堆""" # 判空处理 - assert not self.is_empty() + if self.is_empty(): + raise IndexError("堆为空") # 交换根节点与最右叶节点(即交换首元素与尾元素) self.swap(0, self.size() - 1) # 删除节点 diff --git a/codes/python/chapter_stack_and_queue/array_deque.py b/codes/python/chapter_stack_and_queue/array_deque.py index e623ab721..6891250e4 100644 --- a/codes/python/chapter_stack_and_queue/array_deque.py +++ b/codes/python/chapter_stack_and_queue/array_deque.py @@ -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] diff --git a/codes/python/chapter_stack_and_queue/array_queue.py b/codes/python/chapter_stack_and_queue/array_queue.py index 5a92bc9b5..d7bcaf29c 100644 --- a/codes/python/chapter_stack_and_queue/array_queue.py +++ b/codes/python/chapter_stack_and_queue/array_queue.py @@ -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]: diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index 487b50012..cc259f733 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -26,12 +26,14 @@ class ArrayStack: def pop(self) -> int: """出栈""" - assert not self.is_empty(), "栈为空" + if self.is_empty(): + raise IndexError("栈为空") return self.__stack.pop() def peek(self) -> int: """访问栈顶元素""" - assert not self.is_empty(), "栈为空" + if self.is_empty(): + raise IndexError("栈为空") return self.__stack[-1] def to_list(self) -> list[int]: diff --git a/codes/python/chapter_tree/binary_tree_bfs.py b/codes/python/chapter_tree/binary_tree_bfs.py index a09f86001..f24617bb6 100644 --- a/codes/python/chapter_tree/binary_tree_bfs.py +++ b/codes/python/chapter_tree/binary_tree_bfs.py @@ -39,4 +39,3 @@ if __name__ == "__main__": # 层序遍历 res: list[int] = level_order(root) print("\n层序遍历的节点打印序列 = ", res) - assert res == [1, 2, 3, 4, 5, 6, 7] diff --git a/codes/python/chapter_tree/binary_tree_dfs.py b/codes/python/chapter_tree/binary_tree_dfs.py index 885dbff1c..fb77d5dba 100644 --- a/codes/python/chapter_tree/binary_tree_dfs.py +++ b/codes/python/chapter_tree/binary_tree_dfs.py @@ -52,16 +52,13 @@ if __name__ == "__main__": res = [] pre_order(root) print("\n前序遍历的节点打印序列 = ", res) - assert res == [1, 2, 4, 5, 3, 6, 7] # 中序遍历 res.clear() in_order(root) print("\n中序遍历的节点打印序列 = ", res) - assert res == [4, 2, 5, 1, 6, 3, 7] # 后序遍历 res.clear() post_order(root) print("\n后序遍历的节点打印序列 = ", res) - assert res == [4, 5, 2, 6, 7, 3, 1]