diff --git a/codes/python/chapter_array_and_linkedlist/array.py b/codes/python/chapter_array_and_linkedlist/array.py index 98e207d75..7bbccd42b 100644 --- a/codes/python/chapter_array_and_linkedlist/array.py +++ b/codes/python/chapter_array_and_linkedlist/array.py @@ -64,7 +64,7 @@ def find(nums: list[int], target: int) -> int: return -1 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化数组 arr: list[int] = [0] * 5 diff --git a/codes/python/chapter_array_and_linkedlist/linked_list.py b/codes/python/chapter_array_and_linkedlist/linked_list.py index 828546930..799e7d0be 100644 --- a/codes/python/chapter_array_and_linkedlist/linked_list.py +++ b/codes/python/chapter_array_and_linkedlist/linked_list.py @@ -47,7 +47,7 @@ def find(head: ListNode, target: int) -> int: return -1 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化链表 # 初始化各个节点 diff --git a/codes/python/chapter_array_and_linkedlist/list.py b/codes/python/chapter_array_and_linkedlist/list.py index 38f8428d5..d35870879 100644 --- a/codes/python/chapter_array_and_linkedlist/list.py +++ b/codes/python/chapter_array_and_linkedlist/list.py @@ -4,7 +4,7 @@ Created Time: 2022-11-25 Author: Krahets (krahets@163.com) """ -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化列表 arr: list[int] = [1, 3, 2, 5, 4] diff --git a/codes/python/chapter_array_and_linkedlist/my_list.py b/codes/python/chapter_array_and_linkedlist/my_list.py index 377e9e3dd..287ec0dee 100644 --- a/codes/python/chapter_array_and_linkedlist/my_list.py +++ b/codes/python/chapter_array_and_linkedlist/my_list.py @@ -79,7 +79,7 @@ class MyList: return self.__nums[: self.__size] -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化列表 my_list = MyList() diff --git a/codes/python/chapter_computational_complexity/leetcode_two_sum.py b/codes/python/chapter_computational_complexity/leetcode_two_sum.py index 6934e2232..bdf8ef64c 100644 --- a/codes/python/chapter_computational_complexity/leetcode_two_sum.py +++ b/codes/python/chapter_computational_complexity/leetcode_two_sum.py @@ -27,7 +27,7 @@ def two_sum_hash_table(nums: list[int], target: int) -> list[int]: return [] -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # ======= Test Case ======= nums = [2, 7, 11, 15] diff --git a/codes/python/chapter_computational_complexity/space_complexity.py b/codes/python/chapter_computational_complexity/space_complexity.py index c8b19ee37..53e2b4e24 100644 --- a/codes/python/chapter_computational_complexity/space_complexity.py +++ b/codes/python/chapter_computational_complexity/space_complexity.py @@ -73,7 +73,7 @@ def build_tree(n: int) -> TreeNode | None: return root -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": n = 5 # 常数阶 diff --git a/codes/python/chapter_computational_complexity/time_complexity.py b/codes/python/chapter_computational_complexity/time_complexity.py index c5698b372..ec06069c6 100644 --- a/codes/python/chapter_computational_complexity/time_complexity.py +++ b/codes/python/chapter_computational_complexity/time_complexity.py @@ -114,7 +114,7 @@ def factorial_recur(n: int) -> int: return count -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 n = 8 diff --git a/codes/python/chapter_computational_complexity/worst_best_time_complexity.py b/codes/python/chapter_computational_complexity/worst_best_time_complexity.py index 816baa45c..c8785773d 100644 --- a/codes/python/chapter_computational_complexity/worst_best_time_complexity.py +++ b/codes/python/chapter_computational_complexity/worst_best_time_complexity.py @@ -26,7 +26,7 @@ def find_one(nums: list[int]) -> int: return -1 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": for i in range(10): n: int = 100 diff --git a/codes/python/chapter_graph/graph_adjacency_list.py b/codes/python/chapter_graph/graph_adjacency_list.py index 1585205cf..313481f19 100644 --- a/codes/python/chapter_graph/graph_adjacency_list.py +++ b/codes/python/chapter_graph/graph_adjacency_list.py @@ -69,7 +69,7 @@ class GraphAdjList: print(f"{vertex.val}: {tmp},") -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化无向图 v = vals_to_vets([1, 3, 2, 5, 4]) diff --git a/codes/python/chapter_graph/graph_adjacency_matrix.py b/codes/python/chapter_graph/graph_adjacency_matrix.py index c527816d2..3e8680b6a 100644 --- a/codes/python/chapter_graph/graph_adjacency_matrix.py +++ b/codes/python/chapter_graph/graph_adjacency_matrix.py @@ -84,7 +84,7 @@ class GraphAdjMat: print_matrix(self.adj_mat) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化无向图 # 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 diff --git a/codes/python/chapter_graph/graph_bfs.py b/codes/python/chapter_graph/graph_bfs.py index 73a0ce5d7..24ad250cb 100644 --- a/codes/python/chapter_graph/graph_bfs.py +++ b/codes/python/chapter_graph/graph_bfs.py @@ -35,7 +35,7 @@ def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]: return res -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化无向图 v = vals_to_vets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) diff --git a/codes/python/chapter_graph/graph_dfs.py b/codes/python/chapter_graph/graph_dfs.py index f0438a973..aada40787 100644 --- a/codes/python/chapter_graph/graph_dfs.py +++ b/codes/python/chapter_graph/graph_dfs.py @@ -34,7 +34,7 @@ def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]: return res -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化无向图 v = vals_to_vets([0, 1, 2, 3, 4, 5, 6]) diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py index 961924d57..3527fe6a6 100644 --- a/codes/python/chapter_hashing/array_hash_map.py +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -77,7 +77,7 @@ class ArrayHashMap: print(pair.key, "->", pair.val) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化哈希表 mapp = ArrayHashMap() diff --git a/codes/python/chapter_hashing/hash_map.py b/codes/python/chapter_hashing/hash_map.py index 8fc0a637f..f532902dd 100644 --- a/codes/python/chapter_hashing/hash_map.py +++ b/codes/python/chapter_hashing/hash_map.py @@ -9,7 +9,7 @@ import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from modules import * -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化哈希表 mapp = dict[int, str]() diff --git a/codes/python/chapter_heap/heap.py b/codes/python/chapter_heap/heap.py index 31dc286c0..52eb00eec 100644 --- a/codes/python/chapter_heap/heap.py +++ b/codes/python/chapter_heap/heap.py @@ -24,7 +24,7 @@ def test_pop(heap: list, flag: int = 1) -> None: print_heap([flag * val for val in heap]) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化小顶堆 min_heap, flag = [], 1 @@ -35,33 +35,34 @@ if __name__ == "__main__": # Python 的 heapq 模块默认实现小顶堆 # 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆 # 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆 - """ 元素入堆 """ + + # 元素入堆 test_push(max_heap, 1, flag) test_push(max_heap, 3, flag) test_push(max_heap, 2, flag) test_push(max_heap, 5, flag) test_push(max_heap, 4, flag) - """ 获取堆顶元素 """ + # 获取堆顶元素 peek: int = flag * max_heap[0] print(f"\n堆顶元素为 {peek}") - """ 堆顶元素出堆 """ + # 堆顶元素出堆 test_pop(max_heap, flag) test_pop(max_heap, flag) test_pop(max_heap, flag) test_pop(max_heap, flag) test_pop(max_heap, flag) - """ 获取堆大小 """ + # 获取堆大小 size: int = len(max_heap) print(f"\n堆元素数量为 {size}") - """ 判断堆是否为空 """ + # 判断堆是否为空 is_empty: bool = not max_heap print(f"\n堆是否为空 {is_empty}") - """ 输入列表并建堆 """ + # 输入列表并建堆 # 时间复杂度为 O(n) ,而非 O(nlogn) min_heap: list[int] = [1, 3, 2, 5, 4] heapq.heapify(min_heap) diff --git a/codes/python/chapter_heap/my_heap.py b/codes/python/chapter_heap/my_heap.py index effcb40f9..c772f8800 100644 --- a/codes/python/chapter_heap/my_heap.py +++ b/codes/python/chapter_heap/my_heap.py @@ -105,7 +105,7 @@ class MaxHeap: print_heap(self.max_heap) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化大顶堆 max_heap = MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]) diff --git a/codes/python/chapter_searching/binary_search.py b/codes/python/chapter_searching/binary_search.py index 768f56000..f95d39435 100644 --- a/codes/python/chapter_searching/binary_search.py +++ b/codes/python/chapter_searching/binary_search.py @@ -36,7 +36,7 @@ def binary_search1(nums: list[int], target: int) -> int: return -1 # 未找到目标元素,返回 -1 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": target: int = 6 nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92] diff --git a/codes/python/chapter_searching/hashing_search.py b/codes/python/chapter_searching/hashing_search.py index acc2cfb72..b196b08ca 100644 --- a/codes/python/chapter_searching/hashing_search.py +++ b/codes/python/chapter_searching/hashing_search.py @@ -26,7 +26,7 @@ def hashing_search_linkedlist( return mapp.get(target, None) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": target: int = 3 diff --git a/codes/python/chapter_searching/linear_search.py b/codes/python/chapter_searching/linear_search.py index cdc8cfa1e..7203c72a0 100644 --- a/codes/python/chapter_searching/linear_search.py +++ b/codes/python/chapter_searching/linear_search.py @@ -29,7 +29,7 @@ def linear_search_linkedlist(head: ListNode, target: int) -> ListNode | None: return None # 未找到目标节点,返回 None -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": target: int = 3 diff --git a/codes/python/chapter_sorting/bubble_sort.py b/codes/python/chapter_sorting/bubble_sort.py index 0454c561b..4c9dcdd92 100644 --- a/codes/python/chapter_sorting/bubble_sort.py +++ b/codes/python/chapter_sorting/bubble_sort.py @@ -33,7 +33,7 @@ def bubble_sort_with_flag(nums: list[int]) -> None: break # 此轮冒泡未交换任何元素,直接跳出 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": nums: list[int] = [4, 1, 3, 1, 5, 2] bubble_sort(nums) diff --git a/codes/python/chapter_sorting/bucket_sort.py b/codes/python/chapter_sorting/bucket_sort.py index a3952b50d..72ac9c4ce 100644 --- a/codes/python/chapter_sorting/bucket_sort.py +++ b/codes/python/chapter_sorting/bucket_sort.py @@ -6,6 +6,7 @@ Author: Krahets (krahets@163.com) def bucket_sort(nums: list[float]) -> None: + """桶排序""" # 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 k = len(nums) // 2 buckets = [[] for _ in range(k)] diff --git a/codes/python/chapter_sorting/counting_sort.py b/codes/python/chapter_sorting/counting_sort.py index fe1489f47..d370cf56f 100644 --- a/codes/python/chapter_sorting/counting_sort.py +++ b/codes/python/chapter_sorting/counting_sort.py @@ -52,7 +52,7 @@ def counting_sort(nums: list[int]) -> None: nums[i] = res[i] -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4] diff --git a/codes/python/chapter_sorting/insertion_sort.py b/codes/python/chapter_sorting/insertion_sort.py index 1309a229a..c1d93c071 100644 --- a/codes/python/chapter_sorting/insertion_sort.py +++ b/codes/python/chapter_sorting/insertion_sort.py @@ -18,7 +18,7 @@ def insertion_sort(nums: list[int]) -> None: nums[j + 1] = base # 2. 将 base 赋值到正确位置 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": nums: list[int] = [4, 1, 3, 1, 5, 2] insertion_sort(nums) diff --git a/codes/python/chapter_sorting/merge_sort.py b/codes/python/chapter_sorting/merge_sort.py index 4eef65596..010abe8a6 100644 --- a/codes/python/chapter_sorting/merge_sort.py +++ b/codes/python/chapter_sorting/merge_sort.py @@ -49,7 +49,7 @@ def merge_sort(nums: list[int], left: int, right: int) -> None: merge(nums, left, mid, right) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": nums: list[int] = [7, 3, 2, 6, 0, 1, 5, 4] merge_sort(nums, 0, len(nums) - 1) diff --git a/codes/python/chapter_sorting/quick_sort.py b/codes/python/chapter_sorting/quick_sort.py index 6bf9031f7..2119e7a6e 100644 --- a/codes/python/chapter_sorting/quick_sort.py +++ b/codes/python/chapter_sorting/quick_sort.py @@ -112,7 +112,7 @@ class QuickSortTailCall: right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1] -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 快速排序 nums: list[int] = [2, 4, 1, 0, 3, 5] diff --git a/codes/python/chapter_sorting/radix_sort.py b/codes/python/chapter_sorting/radix_sort.py index cfa9b1549..945a335e9 100644 --- a/codes/python/chapter_sorting/radix_sort.py +++ b/codes/python/chapter_sorting/radix_sort.py @@ -50,7 +50,7 @@ def radix_sort(nums: list[int]) -> None: exp *= 10 -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 基数排序 nums = [ diff --git a/codes/python/chapter_stack_and_queue/array_deque.py b/codes/python/chapter_stack_and_queue/array_deque.py index 923b1c81f..e623ab721 100644 --- a/codes/python/chapter_stack_and_queue/array_deque.py +++ b/codes/python/chapter_stack_and_queue/array_deque.py @@ -91,7 +91,7 @@ class ArrayDeque: return res -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化双向队列 deque = ArrayDeque(10) diff --git a/codes/python/chapter_stack_and_queue/array_queue.py b/codes/python/chapter_stack_and_queue/array_queue.py index 86dc714cf..5a92bc9b5 100644 --- a/codes/python/chapter_stack_and_queue/array_queue.py +++ b/codes/python/chapter_stack_and_queue/array_queue.py @@ -59,7 +59,7 @@ class ArrayQueue: return res -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化队列 queue = ArrayQueue(10) diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index b83ac71f2..487b50012 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -39,7 +39,7 @@ class ArrayStack: return self.__stack -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化栈 stack = ArrayStack() diff --git a/codes/python/chapter_stack_and_queue/deque.py b/codes/python/chapter_stack_and_queue/deque.py index 06859a3e4..271716540 100644 --- a/codes/python/chapter_stack_and_queue/deque.py +++ b/codes/python/chapter_stack_and_queue/deque.py @@ -6,7 +6,7 @@ Author: Peng Chen (pengchzn@gmail.com) from collections import deque -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化双向队列 deq: deque[int] = deque() diff --git a/codes/python/chapter_stack_and_queue/linkedlist_deque.py b/codes/python/chapter_stack_and_queue/linkedlist_deque.py index 8518cafe0..f3433b9e9 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_deque.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_deque.py @@ -112,7 +112,7 @@ class LinkedListDeque: return res -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化双向队列 deque = LinkedListDeque() diff --git a/codes/python/chapter_stack_and_queue/linkedlist_queue.py b/codes/python/chapter_stack_and_queue/linkedlist_queue.py index 5b6aadcde..7050c9bf3 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_queue.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_queue.py @@ -66,7 +66,7 @@ class LinkedListQueue: return queue -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化队列 queue = LinkedListQueue() diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index 78391ce01..85de0a91a 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -58,7 +58,7 @@ class LinkedListStack: return arr -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化栈 stack = LinkedListStack() diff --git a/codes/python/chapter_stack_and_queue/queue.py b/codes/python/chapter_stack_and_queue/queue.py index 4779918d0..923070ff6 100644 --- a/codes/python/chapter_stack_and_queue/queue.py +++ b/codes/python/chapter_stack_and_queue/queue.py @@ -6,7 +6,7 @@ Author: Peng Chen (pengchzn@gmail.com) from collections import deque -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化队列 # 在 Python 中,我们一般将双向队列类 deque 看作队列使用 diff --git a/codes/python/chapter_stack_and_queue/stack.py b/codes/python/chapter_stack_and_queue/stack.py index 094e2ae44..1276271d6 100644 --- a/codes/python/chapter_stack_and_queue/stack.py +++ b/codes/python/chapter_stack_and_queue/stack.py @@ -4,7 +4,7 @@ Created Time: 2022-11-29 Author: Peng Chen (pengchzn@gmail.com) """ -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化栈 # Python 没有内置的栈类,可以把 list 当作栈来使用 diff --git a/codes/python/chapter_tree/avl_tree.py b/codes/python/chapter_tree/avl_tree.py index bf787f1dc..e439eaff8 100644 --- a/codes/python/chapter_tree/avl_tree.py +++ b/codes/python/chapter_tree/avl_tree.py @@ -173,7 +173,7 @@ class AVLTree: return cur -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": def test_insert(tree: AVLTree, val: int): diff --git a/codes/python/chapter_tree/binary_search_tree.py b/codes/python/chapter_tree/binary_search_tree.py index d49b9ba9d..4ae45d69a 100644 --- a/codes/python/chapter_tree/binary_search_tree.py +++ b/codes/python/chapter_tree/binary_search_tree.py @@ -136,7 +136,7 @@ class BinarySearchTree: return root -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化二叉搜索树 nums = list(range(1, 16)) # [1, 2, ..., 15] diff --git a/codes/python/chapter_tree/binary_tree.py b/codes/python/chapter_tree/binary_tree.py index 33c4f3b1f..3539432de 100644 --- a/codes/python/chapter_tree/binary_tree.py +++ b/codes/python/chapter_tree/binary_tree.py @@ -10,7 +10,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from modules import * -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化二叉树 # 初始化节点 diff --git a/codes/python/chapter_tree/binary_tree_bfs.py b/codes/python/chapter_tree/binary_tree_bfs.py index 3f9a04a5c..a09f86001 100644 --- a/codes/python/chapter_tree/binary_tree_bfs.py +++ b/codes/python/chapter_tree/binary_tree_bfs.py @@ -28,7 +28,7 @@ def level_order(root: TreeNode | None) -> list[int]: return res -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化二叉树 # 这里借助了一个从数组直接生成二叉树的函数 diff --git a/codes/python/chapter_tree/binary_tree_dfs.py b/codes/python/chapter_tree/binary_tree_dfs.py index ec91a68f5..885dbff1c 100644 --- a/codes/python/chapter_tree/binary_tree_dfs.py +++ b/codes/python/chapter_tree/binary_tree_dfs.py @@ -40,7 +40,7 @@ def post_order(root: TreeNode | None) -> None: res.append(root.val) -""" Driver Code """ +"""Driver Code""" if __name__ == "__main__": # 初始化二叉树 # 这里借助了一个从数组直接生成二叉树的函数 diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 6485dcc2a..3cf14830c 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -29,7 +29,7 @@ === "Python" ```python title="array.py" - """ 初始化数组 """ + # 初始化数组 arr: List[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ] nums: List[int] = [1, 3, 2, 5, 4] ``` @@ -293,7 +293,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex [class]{}-[func]{insert} ``` -删除元素也是类似,如果我们想要删除索引 $i$ 处的元素,则需要把索引 $i$ 之后的元素都向前移动一位。值得注意的是,删除元素后,原先末尾的元素变得“无意义”了,我们无需特意去修改它。 +删除元素也类似,如果我们想要删除索引 $i$ 处的元素,则需要把索引 $i$ 之后的元素都向前移动一位。值得注意的是,删除元素后,原先末尾的元素变得“无意义”了,我们无需特意去修改它。 ![数组删除元素](array.assets/array_remove_element.png) diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 8e6b9f472..70adc71d8 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -1,10 +1,10 @@ # 链表 -内存空间是所有程序的公共资源,排除已被占用的内存空间,空闲内存空间通常散落在内存各处。在上一节中,我们提到存储数组的内存空间必须是连续的,而当我们需要申请一个非常大的数组时,空闲内存中可能没有这么大的连续空间。 +内存空间是所有程序的公共资源,排除已被占用的内存空间,空闲内存空间通常散落在内存各处。在上一节中,我们提到存储数组的内存空间必须是连续的,而当我们需要申请一个非常大的数组时,空闲内存中可能没有这么大的连续空间。与数组相比,链表更具灵活性,它可以被存储在非连续的内存空间中。 -与数组相比,链表更具灵活性,因为它可以存储在非连续的内存空间。「链表 Linked List」是一种线性数据结构,其每个元素都是一个节点对象,各个节点之间通过指针连接,从当前节点通过指针可以访问到下一个节点。由于指针记录了下个节点的内存地址,因此无需保证内存地址的连续性,从而可以将各个节点分散存储在内存各处。 +「链表 Linked List」是一种线性数据结构,其每个元素都是一个节点对象,各个节点之间通过指针连接,从当前节点通过指针可以访问到下一个节点。**由于指针记录了下个节点的内存地址,因此无需保证内存地址的连续性**,从而可以将各个节点分散存储在内存各处。 -链表「节点 Node」包含两项数据,一是节点「值 Value」,二是指向下一节点的「指针 Pointer」,或称指向下一节点的「引用 Reference」。 +链表「节点 Node」包含两项数据,一是节点「值 Value」,二是指向下一节点的「指针 Pointer」,或称「引用 Reference」。 ![链表定义与存储方式](linked_list.assets/linkedlist_definition.png) @@ -33,8 +33,8 @@ === "Python" ```python title="" - """ 链表节点类 """ class ListNode: + """链表节点类""" def __init__(self, val: int): self.val: int = val # 节点值 self.next: Optional[ListNode] = None # 指向下一节点的指针(引用) @@ -201,7 +201,7 @@ === "Python" ```python title="linked_list.py" - """ 初始化链表 1 -> 3 -> 2 -> 5 -> 4 """ + # 初始化链表 1 -> 3 -> 2 -> 5 -> 4 # 初始化各个节点 n0 = ListNode(1) n1 = ListNode(3) @@ -629,8 +629,8 @@ === "Python" ```python title="" - """ 双向链表节点类 """ class ListNode: + """双向链表节点类""" def __init__(self, val: int): self.val: int = val # 节点值 self.next: Optional[ListNode] = None # 指向后继节点的指针(引用) diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 1cc270156..469b96b39 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -33,7 +33,7 @@ === "Python" ```python title="list.py" - """ 初始化列表 """ + # 初始化列表 # 无初始值 list1: List[int] = [] # 有初始值 @@ -131,10 +131,10 @@ === "Python" ```python title="list.py" - """ 访问元素 """ + # 访问元素 num: int = list[1] # 访问索引 1 处的元素 - """ 更新元素 """ + # 更新元素 list[1] = 0 # 将索引 1 处的元素更新为 0 ``` @@ -249,20 +249,20 @@ === "Python" ```python title="list.py" - """ 清空列表 """ + # 清空列表 list.clear() - """ 尾部添加元素 """ + # 尾部添加元素 list.append(1) list.append(3) list.append(2) list.append(5) list.append(4) - """ 中间插入元素 """ + # 中间插入元素 list.insert(3, 6) # 在索引 3 处插入数字 6 - """ 删除元素 """ + # 删除元素 list.pop(3) # 删除索引 3 处的元素 ``` @@ -429,12 +429,12 @@ === "Python" ```python title="list.py" - """ 通过索引遍历列表 """ + # 通过索引遍历列表 count: int = 0 for i in range(len(list)): count += 1 - """ 直接遍历列表元素 """ + # 直接遍历列表元素 count: int = 0 for n in list: count += 1 @@ -567,7 +567,7 @@ === "Python" ```python title="list.py" - """ 拼接两个列表 """ + # 拼接两个列表 list1: List[int] = [6, 8, 7, 10, 9] list += list1 # 将列表 list1 拼接到 list 之后 ``` @@ -647,7 +647,7 @@ === "Python" ```python title="list.py" - """ 排序列表 """ + # 排序列表 list.sort() # 排序后,列表元素从小到大排列 ``` diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 71486dd03..cf6d6f2f4 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -75,14 +75,14 @@ === "Python" ```python title="" - """ 类 """ class Node: + """类""" def __init__(self, x: int): self.val: int = x # 节点值 self.next: Optional[Node] = None # 指向下一节点的指针(引用) - """ 函数 """ def function() -> int: + """函数""" # do something... return 0 @@ -413,13 +413,13 @@ # do something return 0 - """ 循环 O(1) """ def loop(n: int) -> None: + """循环 O(1)""" for _ in range(n): function() - """ 递归 O(n) """ def recur(n: int) -> int: + """递归 O(n)""" if n == 1: return return recur(n - 1) ``` diff --git a/docs/chapter_data_structure/data_and_memory.md b/docs/chapter_data_structure/data_and_memory.md index d7ece0f8c..e1dbfdb00 100644 --- a/docs/chapter_data_structure/data_and_memory.md +++ b/docs/chapter_data_structure/data_and_memory.md @@ -132,7 +132,7 @@ $$ === "Python" ```python title="" - """ Python 的 list 可以自由存储各种基本数据类型和对象 """ + # Python 的 list 可以自由存储各种基本数据类型和对象 list = [0, 0.0, 'a', False] ``` diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 12968eaa0..da1b11afb 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -80,10 +80,10 @@ === "Python" ```python title="hash_map.py" - """ 初始化哈希表 """ + # 初始化哈希表 mapp: Dict = {} - """ 添加操作 """ + # 添加操作 # 在哈希表中添加键值对 (key, value) mapp[12836] = "小哈" mapp[15937] = "小啰" @@ -91,11 +91,11 @@ mapp[13276] = "小法" mapp[10583] = "小鸭" - """ 查询操作 """ + # 查询操作 # 向哈希表输入键 key ,得到值 value name: str = mapp[15937] - """ 删除操作 """ + # 删除操作 # 在哈希表中删除键值对 (key, value) mapp.pop(10583) ``` @@ -271,7 +271,7 @@ === "Python" ```python title="hash_map.py" - """ 遍历哈希表 """ + # 遍历哈希表 # 遍历键值对 key->value for key, value in mapp.items(): print(key, "->", value) diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index c82856880..a1e5d255c 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -125,17 +125,18 @@ # Python 的 heapq 模块默认实现小顶堆 # 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆 # 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆 - """ 元素入堆 """ + + # 元素入堆 heapq.heappush(max_heap, flag * 1) heapq.heappush(max_heap, flag * 3) heapq.heappush(max_heap, flag * 2) heapq.heappush(max_heap, flag * 5) heapq.heappush(max_heap, flag * 4) - """ 获取堆顶元素 """ + # 获取堆顶元素 peek: int = flag * max_heap[0] # 5 - """ 堆顶元素出堆 """ + # 堆顶元素出堆 # 出堆元素会形成一个从大到小的序列 val = flag * heapq.heappop(max_heap) # 5 val = flag * heapq.heappop(max_heap) # 4 @@ -143,13 +144,13 @@ val = flag * heapq.heappop(max_heap) # 2 val = flag * heapq.heappop(max_heap) # 1 - """ 获取堆大小 """ + # 获取堆大小 size: int = len(max_heap) - """ 判断堆是否为空 """ + # 判断堆是否为空 is_empty: bool = not max_heap - """ 输入列表并建堆 """ + # 输入列表并建堆 min_heap: List[int] = [1, 3, 2, 5, 4] heapq.heapify(min_heap) ``` diff --git a/docs/chapter_preface/suggestions.md b/docs/chapter_preface/suggestions.md index 196f64948..b7c345d4c 100644 --- a/docs/chapter_preface/suggestions.md +++ b/docs/chapter_preface/suggestions.md @@ -55,7 +55,7 @@ === "Python" ```python title="" - """ 标题注释,用于标注函数、类、测试样例等 """ + """标题注释,用于标注函数、类、测试样例等""" # 内容注释,用于详解代码 diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index d888d64d1..b4333925a 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -82,28 +82,28 @@ === "Python" ```python title="deque.py" - """ 初始化双向队列 """ + # 初始化双向队列 deque: Deque[int] = collections.deque() - """ 元素入队 """ + # 元素入队 deque.append(2) # 添加至队尾 deque.append(5) deque.append(4) deque.appendleft(3) # 添加至队首 deque.appendleft(1) - """ 访问元素 """ + # 访问元素 front: int = deque[0] # 队首元素 rear: int = deque[-1] # 队尾元素 - """ 元素出队 """ + # 元素出队 pop_front: int = deque.popleft() # 队首元素出队 pop_rear: int = deque.pop() # 队尾元素出队 - """ 获取双向队列的长度 """ + # 获取双向队列的长度 size: int = len(deque) - """ 判断双向队列是否为空 """ + # 判断双向队列是否为空 is_empty: bool = len(deque) == 0 ``` diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index d7f9c9348..f65b4a3ab 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -1,6 +1,6 @@ # 队列 -「队列 Queue」是一种遵循「先入先出 first in, first out」数据操作规则的线性数据结构。顾名思义,队列模拟的是排队现象,即外面的人不断加入队列尾部,而处于队列头部的人不断地离开。 +「队列 Queue」是一种遵循先入先出(first in, first out)数据操作规则的线性数据结构。顾名思义,队列模拟的是排队现象,即外面的人不断加入队列尾部,而处于队列头部的人不断地离开。 我们将队列头部称为「队首」,队列尾部称为「队尾」,将把元素加入队尾的操作称为「入队」,删除队首元素的操作称为「出队」。 @@ -77,28 +77,28 @@ === "Python" ```python title="queue.py" - """ 初始化队列 """ + # 初始化队列 # 在 Python 中,我们一般将双向队列类 deque 看作队列使用 # 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不建议 que: Deque[int] = collections.deque() - """ 元素入队 """ + # 元素入队 que.append(1) que.append(3) que.append(2) que.append(5) que.append(4) - """ 访问队首元素 """ + # 访问队首元素 front: int = que[0]; - """ 元素出队 """ + # 元素出队 pop: int = que.popleft() - """ 获取队列的长度 """ + # 获取队列的长度 size: int = len(que) - """ 判断队列是否为空 """ + # 判断队列是否为空 is_empty: bool = len(que) == 0 ``` diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 0ea89ea63..41896b960 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -1,6 +1,6 @@ # 栈 -「栈 Stack」是一种遵循「先入后出 first in, last out」数据操作规则的线性数据结构。我们可以将栈类比为放在桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。 +「栈 Stack」是一种遵循先入后出(first in, last out)数据操作规则的线性数据结构。我们可以将栈类比为放在桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。 “盘子”是一种形象比喻,我们将盘子替换为任意一种元素(例如整数、字符、对象等),就得到了栈数据结构。 @@ -79,27 +79,27 @@ === "Python" ```python title="stack.py" - """ 初始化栈 """ + # 初始化栈 # Python 没有内置的栈类,可以把 List 当作栈来使用 stack: List[int] = [] - """ 元素入栈 """ + # 元素入栈 stack.append(1) stack.append(3) stack.append(2) stack.append(5) stack.append(4) - """ 访问栈顶元素 """ + # 访问栈顶元素 peek: int = stack[-1] - """ 元素出栈 """ + # 元素出栈 pop: int = stack.pop() - """ 获取栈的长度 """ + # 获取栈的长度 size: int = len(stack) - """ 判断是否为空 """ + # 判断是否为空 is_empty: bool = len(stack) == 0 ``` diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index a15967988..e9b1fd6c3 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -52,8 +52,8 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "Python" ```python title="" - """ AVL 树节点类 """ class TreeNode: + """AVL 树节点类""" def __init__(self, val: int): self.val: int = val # 节点值 self.height: int = 0 # 节点高度 diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 0b6fc990a..073916dc2 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -29,8 +29,8 @@ === "Python" ```python title="" - """ 二叉树节点类 """ class TreeNode: + """二叉树节点类""" def __init__(self, val: int): self.val: int = val # 节点值 self.left: Optional[TreeNode] = None # 左子节点指针 @@ -188,7 +188,7 @@ === "Python" ```python title="binary_tree.py" - """ 初始化二叉树 """ + # 初始化二叉树 # 初始化节点 n1 = TreeNode(val=1) n2 = TreeNode(val=2) @@ -328,7 +328,7 @@ === "Python" ```python title="binary_tree.py" - """ 插入与删除节点 """ + # 插入与删除节点 p = TreeNode(0) # 在 n1 -> n2 中间插入节点 P n1.left = p @@ -502,7 +502,7 @@ === "Python" ```python title="" - """ 二叉树的数组表示 """ + # 二叉树的数组表示 # 直接使用 None 来表示空位 tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] ```