From fe8027e64a8f4b9aa9d77a5e614430461d80235e Mon Sep 17 00:00:00 2001 From: krahets Date: Sun, 23 Apr 2023 14:58:03 +0800 Subject: [PATCH] build --- chapter_array_and_linkedlist/array.md | 36 +++------ chapter_array_and_linkedlist/linked_list.md | 18 ++--- chapter_array_and_linkedlist/list.md | 54 +++++-------- .../backtracking_algorithm.md | 54 +++++-------- chapter_binary_search/binary_search.md | 52 ++++++------ .../space_complexity.md | 36 +++------ .../time_complexity.md | 81 +++++++------------ chapter_graph/graph_operations.md | 80 +++++++----------- chapter_graph/graph_traversal.md | 24 ++---- chapter_hashing/hash_map.md | 51 ++++-------- chapter_heap/build_heap.md | 6 +- chapter_heap/heap.md | 35 +++----- .../replace_linear_by_hashing.md | 18 ++--- chapter_sorting/bubble_sort.md | 24 ++---- chapter_sorting/bucket_sort.md | 18 ++--- chapter_sorting/counting_sort.md | 33 +++----- chapter_sorting/insertion_sort.md | 9 +-- chapter_sorting/merge_sort.md | 9 +-- chapter_sorting/quick_sort.md | 34 +++----- chapter_sorting/radix_sort.md | 29 +++---- chapter_stack_and_queue/deque.md | 12 +-- chapter_stack_and_queue/queue.md | 79 +++++++----------- chapter_stack_and_queue/stack.md | 63 ++++++--------- chapter_tree/avl_tree.md | 63 +++++---------- chapter_tree/binary_search_tree.md | 37 +++------ chapter_tree/binary_tree_traversal.md | 15 ++-- 26 files changed, 344 insertions(+), 626 deletions(-) diff --git a/chapter_array_and_linkedlist/array.md b/chapter_array_and_linkedlist/array.md index 848cbb787..c14c7ee48 100755 --- a/chapter_array_and_linkedlist/array.md +++ b/chapter_array_and_linkedlist/array.md @@ -213,8 +213,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```csharp title="array.cs" /* 随机返回一个数组元素 */ - int randomAccess(int[] nums) - { + int randomAccess(int[] nums) { Random random = new(); // 在区间 [0, nums.Length) 中随机抽取一个数字 int randomIndex = random.Next(nums.Length); @@ -378,13 +377,11 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```csharp title="array.cs" /* 扩展数组长度 */ - int[] extend(int[] nums, int enlarge) - { + int[] extend(int[] nums, int enlarge) { // 初始化一个扩展长度后的数组 int[] res = new int[nums.Length + enlarge]; // 将原数组中的所有元素复制到新数组 - for (int i = 0; i < nums.Length; i++) - { + for (int i = 0; i < nums.Length; i++) { res[i] = nums[i]; } // 返回扩展后的新数组 @@ -529,11 +526,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```csharp title="array.cs" /* 在数组的索引 index 处插入元素 num */ - void insert(int[] nums, int num, int index) - { + void insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 - for (int i = nums.Length - 1; i > index; i--) - { + for (int i = nums.Length - 1; i > index; i--) { nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 @@ -648,11 +643,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```csharp title="array.cs" /* 删除索引 index 处元素 */ - void remove(int[] nums, int index) - { + void remove(int[] nums, int index) { // 把索引 index 之后的所有元素向前移动一位 - for (int i = index; i < nums.Length - 1; i++) - { + for (int i = index; i < nums.Length - 1; i++) { nums[i] = nums[i + 1]; } } @@ -807,17 +800,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```csharp title="array.cs" /* 遍历数组 */ - void traverse(int[] nums) - { + void traverse(int[] nums) { int count = 0; // 通过索引遍历数组 - for (int i = 0; i < nums.Length; i++) - { + for (int i = 0; i < nums.Length; i++) { count++; } // 直接遍历数组 - foreach (int num in nums) - { + foreach (int num in nums) { count++; } } @@ -957,10 +947,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```csharp title="array.cs" /* 在数组中查找指定元素 */ - int find(int[] nums, int target) - { - for (int i = 0; i < nums.Length; i++) - { + int find(int[] nums, int target) { + for (int i = 0; i < nums.Length; i++) { if (nums[i] == target) return i; } diff --git a/chapter_array_and_linkedlist/linked_list.md b/chapter_array_and_linkedlist/linked_list.md index 001b0ad60..25bdc6e0f 100755 --- a/chapter_array_and_linkedlist/linked_list.md +++ b/chapter_array_and_linkedlist/linked_list.md @@ -427,8 +427,7 @@ comments: true ```csharp title="linked_list.cs" /* 在链表的节点 n0 之后插入节点 P */ - void insert(ListNode n0, ListNode P) - { + void insert(ListNode n0, ListNode P) { ListNode? n1 = n0.next; P.next = n1; n0.next = P; @@ -570,8 +569,7 @@ comments: true ```csharp title="linked_list.cs" /* 删除链表的节点 n0 之后的首个节点 */ - void remove(ListNode n0) - { + void remove(ListNode n0) { if (n0.next == null) return; // n0 -> P -> n1 @@ -716,10 +714,8 @@ comments: true ```csharp title="linked_list.cs" /* 访问链表中索引为 index 的节点 */ - ListNode? access(ListNode head, int index) - { - for (int i = 0; i < index; i++) - { + ListNode? access(ListNode head, int index) { + for (int i = 0; i < index; i++) { if (head == null) return null; head = head.next; @@ -882,11 +878,9 @@ comments: true ```csharp title="linked_list.cs" /* 在链表中查找值为 target 的首个节点 */ - int find(ListNode head, int target) - { + int find(ListNode head, int target) { int index = 0; - while (head != null) - { + while (head != null) { if (head.val == target) return index; head = head.next; diff --git a/chapter_array_and_linkedlist/list.md b/chapter_array_and_linkedlist/list.md index a524d6838..cd6935e99 100755 --- a/chapter_array_and_linkedlist/list.md +++ b/chapter_array_and_linkedlist/list.md @@ -954,12 +954,14 @@ comments: true 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: @@ -972,7 +974,8 @@ comments: true 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() @@ -985,7 +988,8 @@ comments: true 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): @@ -1428,34 +1432,29 @@ comments: true ```csharp title="my_list.cs" /* 列表类简易实现 */ - class MyList - { + class MyList { private int[] nums; // 数组(存储列表元素) private int numsCapacity = 10; // 列表容量 private int numsSize = 0; // 列表长度(即当前元素数量) private int extendRatio = 2; // 每次列表扩容的倍数 /* 构造方法 */ - public MyList() - { + public MyList() { nums = new int[numsCapacity]; } /* 获取列表长度(即当前元素数量)*/ - public int size() - { + public int size() { return numsSize; } /* 获取列表容量 */ - public int capacity() - { + public int capacity() { return numsCapacity; } /* 访问元素 */ - public int get(int index) - { + public int get(int index) { // 索引如果越界则抛出异常,下同 if (index < 0 || index >= numsSize) throw new IndexOutOfRangeException("索引越界"); @@ -1463,16 +1462,14 @@ comments: true } /* 更新元素 */ - public void set(int index, int num) - { + public void set(int index, int num) { if (index < 0 || index >= numsSize) throw new IndexOutOfRangeException("索引越界"); nums[index] = num; } /* 尾部添加元素 */ - public void add(int num) - { + public void add(int num) { // 元素数量超出容量时,触发扩容机制 if (numsSize == numsCapacity) extendCapacity(); @@ -1482,16 +1479,14 @@ comments: true } /* 中间插入元素 */ - public void insert(int index, int num) - { + public void insert(int index, int num) { if (index < 0 || index >= numsSize) throw new IndexOutOfRangeException("索引越界"); // 元素数量超出容量时,触发扩容机制 if (numsSize == numsCapacity) extendCapacity(); // 将索引 index 以及之后的元素都向后移动一位 - for (int j = numsSize - 1; j >= index; j--) - { + for (int j = numsSize - 1; j >= index; j--) { nums[j + 1] = nums[j]; } nums[index] = num; @@ -1500,14 +1495,12 @@ comments: true } /* 删除元素 */ - public int remove(int index) - { + public int remove(int index) { if (index < 0 || index >= numsSize) throw new IndexOutOfRangeException("索引越界"); int num = nums[index]; // 将索引 index 之后的元素都向前移动一位 - for (int j = index; j < numsSize - 1; j++) - { + for (int j = index; j < numsSize - 1; j++) { nums[j] = nums[j + 1]; } // 更新元素数量 @@ -1517,8 +1510,7 @@ comments: true } /* 列表扩容 */ - public void extendCapacity() - { + public void extendCapacity() { // 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组 System.Array.Resize(ref nums, numsCapacity * extendRatio); // 更新列表容量 @@ -1526,12 +1518,10 @@ comments: true } /* 将列表转换为数组 */ - public int[] toArray() - { + public int[] toArray() { // 仅转换有效长度范围内的列表元素 int[] nums = new int[numsSize]; - for (int i = 0; i < numsSize; i++) - { + for (int i = 0; i < numsSize; i++) { nums[i] = get(i); } return nums; diff --git a/chapter_backtracking/backtracking_algorithm.md b/chapter_backtracking/backtracking_algorithm.md index 62f0cf6bb..b40bdad2b 100644 --- a/chapter_backtracking/backtracking_algorithm.md +++ b/chapter_backtracking/backtracking_algorithm.md @@ -88,14 +88,11 @@ comments: true ```csharp title="preorder_traversal_i_compact.cs" /* 前序遍历:例题一 */ - void preOrder(TreeNode root) - { - if (root == null) - { + void preOrder(TreeNode root) { + if (root == null) { return; } - if (root.val == 7) - { + if (root.val == 7) { // 记录解 res.Add(root); } @@ -220,16 +217,13 @@ comments: true ```csharp title="preorder_traversal_ii_compact.cs" /* 前序遍历:例题二 */ - void preOrder(TreeNode root) - { - if (root == null) - { + void preOrder(TreeNode root) { + if (root == null) { return; } // 尝试 path.Add(root); - if (root.val == 7) - { + if (root.val == 7) { // 记录解 res.Add(new List(path)); } @@ -386,17 +380,14 @@ comments: true ```csharp title="preorder_traversal_iii_compact.cs" /* 前序遍历:例题三 */ - void preOrder(TreeNode root) - { + void preOrder(TreeNode root) { // 剪枝 - if (root == null || root.val == 3) - { + if (root == null || root.val == 3) { return; } // 尝试 path.Add(root); - if (root.val == 7) - { + if (root.val == 7) { // 记录解 res.Add(new List(path)); } @@ -679,51 +670,42 @@ def backtrack(state, choices, res): ```csharp title="preorder_traversal_iii_template.cs" /* 判断当前状态是否为解 */ - bool isSolution(List state) - { + bool isSolution(List state) { return state.Count != 0 && state[^1].val == 7; } /* 记录解 */ - void recordSolution(List state, List> res) - { + void recordSolution(List state, List> res) { res.Add(new List(state)); } /* 判断在当前状态下,该选择是否合法 */ - bool isValid(List state, TreeNode choice) - { + bool isValid(List state, TreeNode choice) { return choice != null && choice.val != 3; } /* 更新状态 */ - void makeChoice(List state, TreeNode choice) - { + void makeChoice(List state, TreeNode choice) { state.Add(choice); } /* 恢复状态 */ - void undoChoice(List state, TreeNode choice) - { + void undoChoice(List state, TreeNode choice) { state.RemoveAt(state.Count - 1); } /* 回溯算法:例题三 */ - void backtrack(List state, List choices, List> res) - { + void backtrack(List state, List choices, List> res) { // 检查是否为解 - if (isSolution(state)) - { + if (isSolution(state)) { // 记录解 recordSolution(state, res); return; } // 遍历所有选择 - foreach (TreeNode choice in choices) - { + foreach (TreeNode choice in choices) { // 剪枝:检查选择是否合法 - if (isValid(state, choice)) - { + if (isValid(state, choice)) { // 尝试:做出选择,更新状态 makeChoice(state, choice); List nextChoices = new List() { choice.left, choice.right }; diff --git a/chapter_binary_search/binary_search.md b/chapter_binary_search/binary_search.md index 2a7f5bc69..8cb539767 100755 --- a/chapter_binary_search/binary_search.md +++ b/chapter_binary_search/binary_search.md @@ -186,17 +186,17 @@ $$ ```c title="binary_search.c" /* 二分查找(双闭区间) */ int binarySearch(int *nums, int len, int target) { - // 初始化双闭区间 [0, n-1] ,即 left, right 分别指向数组首元素、尾元素 - int left = 0, right = len - 1; - // 循环,当搜索区间为空时跳出(当 left > right 时为空) - while (left <= right) { - int mid = (left + right) / 2; // 计算中点索引 mid - if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right] 中 - left = mid + 1; - else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid-1] 中 - right = mid - 1; + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + int i = 0, j = len - 1; + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + while (i <= j) { + int m = (i + j) / 2; // 计算中点索引 m + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 + i = m + 1; + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + j = m - 1; else // 找到目标元素,返回其索引 - return mid; + return m; } // 未找到目标元素,返回 -1 return -1; @@ -207,13 +207,11 @@ $$ ```csharp title="binary_search.cs" /* 二分查找(双闭区间) */ - int binarySearch(int[] nums, int target) - { + int binarySearch(int[] nums, int target) { // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 int i = 0, j = nums.Length - 1; // 循环,当搜索区间为空时跳出(当 i > j 时为空) - while (i <= j) - { + while (i <= j) { int m = (i + j) / 2; // 计算中点索引 m if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1; @@ -510,17 +508,17 @@ $$ ```c title="binary_search.c" /* 二分查找(左闭右开) */ int binarySearch1(int *nums, int len, int target) { - // 初始化左闭右开 [0, n) ,即 left, right 分别指向数组首元素、尾元素+1 - int left = 0, right = len; - // 循环,当搜索区间为空时跳出(当 left = right 时为空) - while (left < right) { - int mid = (left + right) / 2; // 计算中点索引 mid - if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right) 中 - left = mid + 1; - else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid) 中 - right = mid; + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + int i = 0, j = len; + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + while (i < j) { + int m = (i + j) / 2; // 计算中点索引 m + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + i = m + 1; + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 + j = m; else // 找到目标元素,返回其索引 - return mid; + return m; } // 未找到目标元素,返回 -1 return -1; @@ -531,13 +529,11 @@ $$ ```csharp title="binary_search.cs" /* 二分查找(左闭右开) */ - int binarySearch1(int[] nums, int target) - { + int binarySearch1(int[] nums, int target) { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 int i = 0, j = nums.Length; // 循环,当搜索区间为空时跳出(当 i = j 时为空) - while (i < j) - { + while (i < j) { int m = (i + j) / 2; // 计算中点索引 m if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 i = m + 1; diff --git a/chapter_computational_complexity/space_complexity.md b/chapter_computational_complexity/space_complexity.md index 527b828fa..626ca3b19 100755 --- a/chapter_computational_complexity/space_complexity.md +++ b/chapter_computational_complexity/space_complexity.md @@ -760,21 +760,18 @@ $$ ```csharp title="space_complexity.cs" /* 常数阶 */ - void constant(int n) - { + void constant(int n) { // 常量、变量、对象占用 O(1) 空间 int a = 0; int b = 0; int[] nums = new int[10000]; ListNode node = new ListNode(0); // 循环中的变量占用 O(1) 空间 - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int c = 0; } // 循环中的函数占用 O(1) 空间 - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { function(); } } @@ -997,20 +994,17 @@ $$ ```csharp title="space_complexity.cs" /* 线性阶 */ - void linear(int n) - { + void linear(int n) { // 长度为 n 的数组占用 O(n) 空间 int[] nums = new int[n]; // 长度为 n 的列表占用 O(n) 空间 List nodes = new(); - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { nodes.Add(new ListNode(i)); } // 长度为 n 的哈希表占用 O(n) 空间 Dictionary map = new(); - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { map.Add(i, i.ToString()); } } @@ -1145,8 +1139,7 @@ $$ ```csharp title="space_complexity.cs" /* 线性阶(递归实现) */ - void linearRecur(int n) - { + void linearRecur(int n) { Console.WriteLine("递归 n = " + n); if (n == 1) return; linearRecur(n - 1); @@ -1312,17 +1305,14 @@ $$ ```csharp title="space_complexity.cs" /* 平方阶 */ - void quadratic(int n) - { + void quadratic(int n) { // 矩阵占用 O(n^2) 空间 int[,] numMatrix = new int[n, n]; // 二维列表占用 O(n^2) 空间 List> numList = new(); - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { List tmp = new(); - for (int j = 0; j < n; j++) - { + for (int j = 0; j < n; j++) { tmp.Add(0); } numList.Add(tmp); @@ -1458,8 +1448,7 @@ $$ ```csharp title="space_complexity.cs" /* 平方阶(递归实现) */ - int quadraticRecur(int n) - { + int quadraticRecur(int n) { if (n <= 0) return 0; int[] nums = new int[n]; Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length); @@ -1603,8 +1592,7 @@ $$ ```csharp title="space_complexity.cs" /* 指数阶(建立满二叉树) */ - TreeNode? buildTree(int n) - { + TreeNode? buildTree(int n) { if (n == 0) return null; TreeNode root = new TreeNode(0); root.left = buildTree(n - 1); diff --git a/chapter_computational_complexity/time_complexity.md b/chapter_computational_complexity/time_complexity.md index bd4e45006..deac7acc1 100755 --- a/chapter_computational_complexity/time_complexity.md +++ b/chapter_computational_complexity/time_complexity.md @@ -885,8 +885,7 @@ $$ ```csharp title="time_complexity.cs" /* 常数阶 */ - int constant(int n) - { + int constant(int n) { int count = 0; int size = 100000; for (int i = 0; i < size; i++) @@ -1016,8 +1015,7 @@ $$ ```csharp title="time_complexity.cs" /* 线性阶 */ - int linear(int n) - { + int linear(int n) { int count = 0; for (int i = 0; i < n; i++) count++; @@ -1158,12 +1156,10 @@ $$ ```csharp title="time_complexity.cs" /* 线性阶(遍历数组) */ - int arrayTraversal(int[] nums) - { + int arrayTraversal(int[] nums) { int count = 0; // 循环次数与数组长度成正比 - foreach (int num in nums) - { + foreach (int num in nums) { count++; } return count; @@ -1315,14 +1311,11 @@ $$ ```csharp title="time_complexity.cs" /* 平方阶 */ - int quadratic(int n) - { + int quadratic(int n) { int count = 0; // 循环次数与数组长度成平方关系 - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { count++; } } @@ -1535,17 +1528,13 @@ $$ ```csharp title="time_complexity.cs" /* 平方阶(冒泡排序) */ - int bubbleSort(int[] nums) - { + int bubbleSort(int[] nums) { int count = 0; // 计数器 // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (int i = nums.Length - 1; i > 0; i--) - { + for (int i = nums.Length - 1; i > 0; i--) { // 内循环:冒泡操作 - for (int j = 0; j < i; j++) - { - if (nums[j] > nums[j + 1]) - { + for (int j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] int tmp = nums[j]; nums[j] = nums[j + 1]; @@ -1745,14 +1734,11 @@ $$ ```csharp title="time_complexity.cs" /* 指数阶(循环实现) */ - int exponential(int n) - { + int exponential(int n) { int count = 0, bas = 1; // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) - for (int i = 0; i < n; i++) - { - for (int j = 0; j < bas; j++) - { + for (int i = 0; i < n; i++) { + for (int j = 0; j < bas; j++) { count++; } bas *= 2; @@ -1887,8 +1873,7 @@ $$ ```csharp title="time_complexity.cs" /* 指数阶(递归实现) */ - int expRecur(int n) - { + int expRecur(int n) { if (n == 1) return 1; return expRecur(n - 1) + expRecur(n - 1) + 1; } @@ -2024,11 +2009,9 @@ $$ ```csharp title="time_complexity.cs" /* 对数阶(循环实现) */ - int logarithmic(float n) - { + int logarithmic(float n) { int count = 0; - while (n > 1) - { + while (n > 1) { n = n / 2; count++; } @@ -2152,8 +2135,7 @@ $$ ```csharp title="time_complexity.cs" /* 对数阶(递归实现) */ - int logRecur(float n) - { + int logRecur(float n) { if (n <= 1) return 0; return logRecur(n / 2) + 1; } @@ -2295,13 +2277,11 @@ $$ ```csharp title="time_complexity.cs" /* 线性对数阶 */ - int linearLogRecur(float n) - { + int linearLogRecur(float n) { if (n <= 1) return 1; int count = linearLogRecur(n / 2) + linearLogRecur(n / 2); - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { count++; } return count; @@ -2466,13 +2446,11 @@ $$ ```csharp title="time_complexity.cs" /* 阶乘阶(递归实现) */ - int factorialRecur(int n) - { + int factorialRecur(int n) { if (n == 0) return 1; int count = 0; // 从 1 个分裂出 n 个 - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { count += factorialRecur(n - 1); } return count; @@ -2740,18 +2718,15 @@ $$ ```csharp title="worst_best_time_complexity.cs" /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ - int[] randomNumbers(int n) - { + int[] randomNumbers(int n) { int[] nums = new int[n]; // 生成数组 nums = { 1, 2, 3, ..., n } - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { nums[i] = i + 1; } // 随机打乱数组元素 - for (int i = 0; i < nums.Length; i++) - { + for (int i = 0; i < nums.Length; i++) { var index = new Random().Next(i, nums.Length); var tmp = nums[i]; var ran = nums[index]; @@ -2762,10 +2737,8 @@ $$ } /* 查找数组 nums 中数字 1 所在索引 */ - int findOne(int[] nums) - { - for (int i = 0; i < nums.Length; i++) - { + int findOne(int[] nums) { + for (int i = 0; i < nums.Length; i++) { // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) diff --git a/chapter_graph/graph_operations.md b/chapter_graph/graph_operations.md index 38bd19567..07b28338b 100644 --- a/chapter_graph/graph_operations.md +++ b/chapter_graph/graph_operations.md @@ -580,58 +580,49 @@ comments: true ```csharp title="graph_adjacency_matrix.cs" /* 基于邻接矩阵实现的无向图类 */ - class GraphAdjMat - { + class GraphAdjMat { List vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” List> adjMat; // 邻接矩阵,行列索引对应“顶点索引” /* 构造函数 */ - public GraphAdjMat(int[] vertices, int[][] edges) - { + public GraphAdjMat(int[] vertices, int[][] edges) { this.vertices = new List(); this.adjMat = new List>(); // 添加顶点 - foreach (int val in vertices) - { + foreach (int val in vertices) { addVertex(val); } // 添加边 // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 - foreach (int[] e in edges) - { + foreach (int[] e in edges) { addEdge(e[0], e[1]); } } /* 获取顶点数量 */ - public int size() - { + public int size() { return vertices.Count; } /* 添加顶点 */ - public void addVertex(int val) - { + public void addVertex(int val) { int n = size(); // 向顶点列表中添加新顶点的值 vertices.Add(val); // 在邻接矩阵中添加一行 List newRow = new List(n); - for (int j = 0; j < n; j++) - { + for (int j = 0; j < n; j++) { newRow.Add(0); } adjMat.Add(newRow); // 在邻接矩阵中添加一列 - foreach (List row in adjMat) - { + foreach (List row in adjMat) { row.Add(0); } } /* 删除顶点 */ - public void removeVertex(int index) - { + public void removeVertex(int index) { if (index >= size()) throw new IndexOutOfRangeException(); // 在顶点列表中移除索引 index 的顶点 @@ -639,16 +630,14 @@ comments: true // 在邻接矩阵中删除索引 index 的行 adjMat.RemoveAt(index); // 在邻接矩阵中删除索引 index 的列 - foreach (List row in adjMat) - { + foreach (List row in adjMat) { row.RemoveAt(index); } } /* 添加边 */ // 参数 i, j 对应 vertices 元素索引 - public void addEdge(int i, int j) - { + public void addEdge(int i, int j) { // 索引越界与相等处理 if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw new IndexOutOfRangeException(); @@ -659,18 +648,16 @@ comments: true /* 删除边 */ // 参数 i, j 对应 vertices 元素索引 - public void removeEdge(int i, int j) - { + public void removeEdge(int i, int j) { // 索引越界与相等处理 if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw new IndexOutOfRangeException(); adjMat[i][j] = 0; adjMat[j][i] = 0; } - + /* 打印邻接矩阵 */ - public void print() - { + public void print() { Console.Write("顶点列表 = "); PrintUtil.PrintList(vertices); Console.WriteLine("邻接矩阵 ="); @@ -989,7 +976,7 @@ comments: true 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) @@ -997,7 +984,7 @@ comments: true 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) @@ -1012,7 +999,7 @@ comments: true 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 的边 @@ -1307,18 +1294,15 @@ comments: true ```csharp title="graph_adjacency_list.cs" /* 基于邻接表实现的无向图类 */ - class GraphAdjList - { + class GraphAdjList { // 邻接表,key: 顶点,value:该顶点的所有邻接顶点 public Dictionary> adjList; /* 构造函数 */ - public GraphAdjList(Vertex[][] edges) - { + public GraphAdjList(Vertex[][] edges) { this.adjList = new Dictionary>(); // 添加所有顶点和边 - foreach (Vertex[] edge in edges) - { + foreach (Vertex[] edge in edges) { addVertex(edge[0]); addVertex(edge[1]); addEdge(edge[0], edge[1]); @@ -1326,14 +1310,12 @@ comments: true } /* 获取顶点数量 */ - public int size() - { + public int size() { return adjList.Count; } /* 添加边 */ - public void addEdge(Vertex vet1, Vertex vet2) - { + public void addEdge(Vertex vet1, Vertex vet2) { if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2) throw new InvalidOperationException(); // 添加边 vet1 - vet2 @@ -1342,8 +1324,7 @@ comments: true } /* 删除边 */ - public void removeEdge(Vertex vet1, Vertex vet2) - { + public void removeEdge(Vertex vet1, Vertex vet2) { if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2) throw new InvalidOperationException(); // 删除边 vet1 - vet2 @@ -1352,8 +1333,7 @@ comments: true } /* 添加顶点 */ - public void addVertex(Vertex vet) - { + public void addVertex(Vertex vet) { if (adjList.ContainsKey(vet)) return; // 在邻接表中添加一个新链表 @@ -1361,25 +1341,21 @@ comments: true } /* 删除顶点 */ - public void removeVertex(Vertex vet) - { + public void removeVertex(Vertex vet) { if (!adjList.ContainsKey(vet)) throw new InvalidOperationException(); // 在邻接表中删除顶点 vet 对应的链表 adjList.Remove(vet); // 遍历其他顶点的链表,删除所有包含 vet 的边 - foreach (List list in adjList.Values) - { + foreach (List list in adjList.Values) { list.Remove(vet); } } /* 打印邻接表 */ - public void print() - { + public void print() { Console.WriteLine("邻接表 ="); - foreach (KeyValuePair> entry in adjList) - { + foreach (KeyValuePair> entry in adjList) { List tmp = new List(); foreach (Vertex vertex in entry.Value) tmp.Add(vertex.val); diff --git a/chapter_graph/graph_traversal.md b/chapter_graph/graph_traversal.md index d4a251438..d976ea3ba 100644 --- a/chapter_graph/graph_traversal.md +++ b/chapter_graph/graph_traversal.md @@ -225,8 +225,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质 ```csharp title="graph_bfs.cs" /* 广度优先遍历 BFS */ // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 - List graphBFS(GraphAdjList graph, Vertex startVet) - { + List graphBFS(GraphAdjList graph, Vertex startVet) { // 顶点遍历序列 List res = new List(); // 哈希表,用于记录已被访问过的顶点 @@ -235,14 +234,11 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质 Queue que = new Queue(); que.Enqueue(startVet); // 以顶点 vet 为起点,循环直至访问完所有顶点 - while (que.Count > 0) - { + while (que.Count > 0) { Vertex vet = que.Dequeue(); // 队首顶点出队 res.Add(vet); // 记录访问顶点 - foreach (Vertex adjVet in graph.adjList[vet]) - { - if (visited.Contains(adjVet)) - { + foreach (Vertex adjVet in graph.adjList[vet]) { + if (visited.Contains(adjVet)) { continue; // 跳过已被访问过的顶点 } que.Enqueue(adjVet); // 只入队未访问的顶点 @@ -535,15 +531,12 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质 ```csharp title="graph_dfs.cs" /* 深度优先遍历 DFS 辅助函数 */ - void dfs(GraphAdjList graph, HashSet visited, List res, Vertex vet) - { + void dfs(GraphAdjList graph, HashSet visited, List res, Vertex vet) { res.Add(vet); // 记录访问顶点 visited.Add(vet); // 标记该顶点已被访问 // 遍历该顶点的所有邻接顶点 - foreach (Vertex adjVet in graph.adjList[vet]) - { - if (visited.Contains(adjVet)) - { + foreach (Vertex adjVet in graph.adjList[vet]) { + if (visited.Contains(adjVet)) { continue; // 跳过已被访问过的顶点 } // 递归访问邻接顶点 @@ -553,8 +546,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质 /* 深度优先遍历 DFS */ // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 - List graphDFS(GraphAdjList graph, Vertex startVet) - { + List graphDFS(GraphAdjList graph, Vertex startVet) { // 顶点遍历序列 List res = new List(); // 哈希表,用于记录已被访问过的顶点 diff --git a/chapter_hashing/hash_map.md b/chapter_hashing/hash_map.md index c660a3caa..f1908bcf3 100755 --- a/chapter_hashing/hash_map.md +++ b/chapter_hashing/hash_map.md @@ -971,41 +971,34 @@ $$ ```csharp title="array_hash_map.cs" /* 键值对 int->string */ - class Entry - { + class Entry { public int key; public string val; - public Entry(int key, string val) - { + public Entry(int key, string val) { this.key = key; this.val = val; } } /* 基于数组简易实现的哈希表 */ - class ArrayHashMap - { + class ArrayHashMap { private List buckets; - public ArrayHashMap() - { + public ArrayHashMap() { // 初始化数组,包含 100 个桶 buckets = new(); - for (int i = 0; i < 100; i++) - { + for (int i = 0; i < 100; i++) { buckets.Add(null); } } /* 哈希函数 */ - private int hashFunc(int key) - { + private int hashFunc(int key) { int index = key % 100; return index; } /* 查询操作 */ - public string? get(int key) - { + public string? get(int key) { int index = hashFunc(key); Entry? pair = buckets[index]; if (pair == null) return null; @@ -1013,27 +1006,23 @@ $$ } /* 添加操作 */ - public void put(int key, string val) - { + public void put(int key, string val) { Entry pair = new Entry(key, val); int index = hashFunc(key); buckets[index] = pair; } /* 删除操作 */ - public void remove(int key) - { + public void remove(int key) { int index = hashFunc(key); // 置为 null ,代表删除 buckets[index] = null; } /* 获取所有键值对 */ - public List entrySet() - { + public List entrySet() { List entrySet = new(); - foreach (Entry? pair in buckets) - { + foreach (Entry? pair in buckets) { if (pair != null) entrySet.Add(pair); } @@ -1041,11 +1030,9 @@ $$ } /* 获取所有键 */ - public List keySet() - { + public List keySet() { List keySet = new(); - foreach (Entry? pair in buckets) - { + foreach (Entry? pair in buckets) { if (pair != null) keySet.Add(pair.key); } @@ -1053,11 +1040,9 @@ $$ } /* 获取所有值 */ - public List valueSet() - { + public List valueSet() { List valueSet = new(); - foreach (Entry? pair in buckets) - { + foreach (Entry? pair in buckets) { if (pair != null) valueSet.Add(pair.val); } @@ -1065,10 +1050,8 @@ $$ } /* 打印哈希表 */ - public void print() - { - foreach (Entry kv in entrySet()) - { + public void print() { + foreach (Entry kv in entrySet()) { Console.WriteLine(kv.key + " -> " + kv.val); } } diff --git a/chapter_heap/build_heap.md b/chapter_heap/build_heap.md index e98331fca..86a06b5b2 100644 --- a/chapter_heap/build_heap.md +++ b/chapter_heap/build_heap.md @@ -120,14 +120,12 @@ comments: true ```csharp title="my_heap.cs" /* 构造函数,根据输入列表建堆 */ - MaxHeap(IEnumerable nums) - { + MaxHeap(IEnumerable nums) { // 将列表元素原封不动添加进堆 maxHeap = new List(nums); // 堆化除叶节点以外的其他所有节点 var size = parent(this.size() - 1); - for (int i = size; i >= 0; i--) - { + for (int i = size; i >= 0; i--) { siftDown(i); } } diff --git a/chapter_heap/heap.md b/chapter_heap/heap.md index b08710ba7..454f5a83d 100644 --- a/chapter_heap/heap.md +++ b/chapter_heap/heap.md @@ -460,20 +460,17 @@ comments: true ```csharp title="my_heap.cs" /* 获取左子节点索引 */ - int left(int i) - { + int left(int i) { return 2 * i + 1; } /* 获取右子节点索引 */ - int right(int i) - { + int right(int i) { return 2 * i + 2; } /* 获取父节点索引 */ - int parent(int i) - { + int parent(int i) { return (i - 1) / 2; // 向下整除 } ``` @@ -587,8 +584,7 @@ comments: true ```csharp title="my_heap.cs" /* 访问堆顶元素 */ - int peek() - { + int peek() { return maxHeap[0]; } ``` @@ -834,8 +830,7 @@ comments: true ```csharp title="my_heap.cs" /* 元素入堆 */ - void push(int val) - { + void push(int val) { // 添加节点 maxHeap.Add(val); // 从底至顶堆化 @@ -843,10 +838,8 @@ comments: true } /* 从节点 i 开始,从底至顶堆化 */ - void siftUp(int i) - { - while (true) - { + void siftUp(int i) { + while (true) { // 获取节点 i 的父节点 int p = parent(i); // 若“越过根节点”或“节点无需修复”,则结束堆化 @@ -965,7 +958,7 @@ comments: true int pop() { // 判空处理 if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); // 交换根节点与最右叶节点(即交换首元素与尾元素) swap(0, size() - 1); // 删除节点 @@ -1039,7 +1032,8 @@ comments: true def pop(self) -> int: """元素出堆""" # 判空处理 - assert not self.is_empty() + if self.is_empty(): + raise IndexError("堆为空") # 交换根节点与最右叶节点(即交换首元素与尾元素) self.swap(0, self.size() - 1) # 删除节点 @@ -1235,8 +1229,7 @@ comments: true ```csharp title="my_heap.cs" /* 元素出堆 */ - int pop() - { + int pop() { // 判空处理 if (isEmpty()) throw new IndexOutOfRangeException(); @@ -1252,10 +1245,8 @@ comments: true } /* 从节点 i 开始,从顶至底堆化 */ - void siftDown(int i) - { - while (true) - { + void siftDown(int i) { + while (true) { // 判断节点 i, l, r 中值最大的节点,记为 ma int l = left(i), r = right(i), ma = i; if (l < size() && maxHeap[l] > maxHeap[ma]) diff --git a/chapter_searching/replace_linear_by_hashing.md b/chapter_searching/replace_linear_by_hashing.md index d267109c8..8f6600d3f 100755 --- a/chapter_searching/replace_linear_by_hashing.md +++ b/chapter_searching/replace_linear_by_hashing.md @@ -145,14 +145,11 @@ comments: true ```csharp title="leetcode_two_sum.cs" /* 方法一:暴力枚举 */ - int[] twoSumBruteForce(int[] nums, int target) - { + int[] twoSumBruteForce(int[] nums, int target) { int size = nums.Length; // 两层循环,时间复杂度 O(n^2) - for (int i = 0; i < size - 1; i++) - { - for (int j = i + 1; j < size; j++) - { + for (int i = 0; i < size - 1; i++) { + for (int j = i + 1; j < size; j++) { if (nums[i] + nums[j] == target) return new int[] { i, j }; } @@ -372,16 +369,13 @@ comments: true ```csharp title="leetcode_two_sum.cs" /* 方法二:辅助哈希表 */ - int[] twoSumHashTable(int[] nums, int target) - { + int[] twoSumHashTable(int[] nums, int target) { int size = nums.Length; // 辅助哈希表,空间复杂度 O(n) Dictionary dic = new(); // 单层循环,时间复杂度 O(n) - for (int i = 0; i < size; i++) - { - if (dic.ContainsKey(target - nums[i])) - { + for (int i = 0; i < size; i++) { + if (dic.ContainsKey(target - nums[i])) { return new int[] { dic[target - nums[i]], i }; } dic.Add(nums[i], i); diff --git a/chapter_sorting/bubble_sort.md b/chapter_sorting/bubble_sort.md index 1e963c989..58b962e43 100755 --- a/chapter_sorting/bubble_sort.md +++ b/chapter_sorting/bubble_sort.md @@ -178,16 +178,12 @@ comments: true ```csharp title="bubble_sort.cs" /* 冒泡排序 */ - void bubbleSort(int[] nums) - { + void bubbleSort(int[] nums) { // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (int i = nums.Length - 1; i > 0; i--) - { + for (int i = nums.Length - 1; i > 0; i--) { // 内循环:冒泡操作 - for (int j = 0; j < i; j++) - { - if (nums[j] > nums[j + 1]) - { + for (int j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] int tmp = nums[j]; nums[j] = nums[j + 1]; @@ -416,17 +412,13 @@ comments: true ```csharp title="bubble_sort.cs" /* 冒泡排序(标志优化)*/ - void bubbleSortWithFlag(int[] nums) - { + void bubbleSortWithFlag(int[] nums) { // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (int i = nums.Length - 1; i > 0; i--) - { + for (int i = nums.Length - 1; i > 0; i--) { bool flag = false; // 初始化标志位 // 内循环:冒泡操作 - for (int j = 0; j < i; j++) - { - if (nums[j] > nums[j + 1]) - { + for (int j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] int tmp = nums[j]; nums[j] = nums[j + 1]; diff --git a/chapter_sorting/bucket_sort.md b/chapter_sorting/bucket_sort.md index 6d8161d0d..bf2089bec 100644 --- a/chapter_sorting/bucket_sort.md +++ b/chapter_sorting/bucket_sort.md @@ -219,35 +219,29 @@ comments: true ```csharp title="bucket_sort.cs" /* 桶排序 */ - void bucketSort(float[] nums) - { + void bucketSort(float[] nums) { // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 int k = nums.Length / 2; List> buckets = new List>(); - for (int i = 0; i < k; i++) - { + for (int i = 0; i < k; i++) { buckets.Add(new List()); } // 1. 将数组元素分配到各个桶中 - foreach (float num in nums) - { + foreach (float num in nums) { // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] int i = (int)num * k; // 将 num 添加进桶 i buckets[i].Add(num); } // 2. 对各个桶执行排序 - foreach (List bucket in buckets) - { + foreach (List bucket in buckets) { // 使用内置排序函数,也可以替换成其他排序算法 bucket.Sort(); } // 3. 遍历桶合并结果 int j = 0; - foreach (List bucket in buckets) - { - foreach (float num in bucket) - { + foreach (List bucket in buckets) { + foreach (float num in bucket) { nums[j++] = num; } } diff --git a/chapter_sorting/counting_sort.md b/chapter_sorting/counting_sort.md index 4145baee4..7cf992bc2 100644 --- a/chapter_sorting/counting_sort.md +++ b/chapter_sorting/counting_sort.md @@ -212,27 +212,22 @@ comments: true ```csharp title="counting_sort.cs" /* 计数排序 */ // 简单实现,无法用于排序对象 - void countingSortNaive(int[] nums) - { + void countingSortNaive(int[] nums) { // 1. 统计数组最大元素 m int m = 0; - foreach (int num in nums) - { + foreach (int num in nums) { m = Math.Max(m, num); } // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 int[] counter = new int[m + 1]; - foreach (int num in nums) - { + foreach (int num in nums) { counter[num]++; } // 3. 遍历 counter ,将各元素填入原数组 nums int i = 0; - for (int num = 0; num < m + 1; num++) - { - for (int j = 0; j < counter[num]; j++, i++) - { + for (int num = 0; num < m + 1; num++) { + for (int j = 0; j < counter[num]; j++, i++) { nums[i] = num; } } @@ -579,40 +574,34 @@ $$ ```csharp title="counting_sort.cs" /* 计数排序 */ // 完整实现,可排序对象,并且是稳定排序 - void countingSort(int[] nums) - { + void countingSort(int[] nums) { // 1. 统计数组最大元素 m int m = 0; - foreach (int num in nums) - { + foreach (int num in nums) { m = Math.Max(m, num); } // 2. 统计各数字的出现次数 // counter[num] 代表 num 的出现次数 int[] counter = new int[m + 1]; - foreach (int num in nums) - { + foreach (int num in nums) { counter[num]++; } // 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引” // 即 counter[num]-1 是 num 在 res 中最后一次出现的索引 - for (int i = 0; i < m; i++) - { + for (int i = 0; i < m; i++) { counter[i + 1] += counter[i]; } // 4. 倒序遍历 nums ,将各元素填入结果数组 res // 初始化数组 res 用于记录结果 int n = nums.Length; int[] res = new int[n]; - for (int i = n - 1; i >= 0; i--) - { + for (int i = n - 1; i >= 0; i--) { int num = nums[i]; res[counter[num] - 1] = num; // 将 num 放置到对应索引处 counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引 } // 使用结果数组 res 覆盖原数组 nums - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { nums[i] = res[i]; } } diff --git a/chapter_sorting/insertion_sort.md b/chapter_sorting/insertion_sort.md index 2060c030e..e2640e49f 100755 --- a/chapter_sorting/insertion_sort.md +++ b/chapter_sorting/insertion_sort.md @@ -157,15 +157,12 @@ comments: true ```csharp title="insertion_sort.cs" /* 插入排序 */ - void insertionSort(int[] nums) - { + void insertionSort(int[] nums) { // 外循环:base = nums[1], nums[2], ..., nums[n-1] - for (int i = 1; i < nums.Length; i++) - { + for (int i = 1; i < nums.Length; i++) { int bas = nums[i], j = i - 1; // 内循环:将 base 插入到左边的正确位置 - while (j >= 0 && nums[j] > bas) - { + while (j >= 0 && nums[j] > bas) { nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 j--; } diff --git a/chapter_sorting/merge_sort.md b/chapter_sorting/merge_sort.md index 34d1488f6..36e25d079 100755 --- a/chapter_sorting/merge_sort.md +++ b/chapter_sorting/merge_sort.md @@ -385,8 +385,7 @@ comments: true /* 合并左子数组和右子数组 */ // 左子数组区间 [left, mid] // 右子数组区间 [mid + 1, right] - void merge(int[] nums, int left, int mid, int right) - { + void merge(int[] nums, int left, int mid, int right) { // 初始化辅助数组 int[] tmp = nums[left..(right + 1)]; // 左子数组的起始索引和结束索引 @@ -396,8 +395,7 @@ comments: true // i, j 分别指向左子数组、右子数组的首元素 int i = leftStart, j = rightStart; // 通过覆盖原数组 nums 来合并左子数组和右子数组 - for (int k = left; k <= right; k++) - { + for (int k = left; k <= right; k++) { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; @@ -411,8 +409,7 @@ comments: true } /* 归并排序 */ - void mergeSort(int[] nums, int left, int right) - { + void mergeSort(int[] nums, int left, int right) { // 终止条件 if (left >= right) return; // 当子数组长度为 1 时终止递归 // 划分阶段 diff --git a/chapter_sorting/quick_sort.md b/chapter_sorting/quick_sort.md index d0942830b..ee8930640 100755 --- a/chapter_sorting/quick_sort.md +++ b/chapter_sorting/quick_sort.md @@ -230,20 +230,17 @@ comments: true ```csharp title="quick_sort.cs" /* 元素交换 */ - void swap(int[] nums, int i, int j) - { + void swap(int[] nums, int i, int j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } /* 哨兵划分 */ - int partition(int[] nums, int left, int right) - { + int partition(int[] nums, int left, int right) { // 以 nums[left] 作为基准数 int i = left, j = right; - while (i < j) - { + while (i < j) { while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素 while (i < j && nums[i] <= nums[left]) @@ -459,8 +456,7 @@ comments: true ```csharp title="quick_sort.cs" /* 快速排序 */ - void quickSort(int[] nums, int left, int right) - { + void quickSort(int[] nums, int left, int right) { // 子数组长度为 1 时终止递归 if (left >= right) return; @@ -815,8 +811,7 @@ comments: true ```csharp title="quick_sort.cs" /* 选取三个元素的中位数 */ - int medianThree(int[] nums, int left, int mid, int right) - { + int medianThree(int[] nums, int left, int mid, int right) { // 此处使用异或运算来简化代码 // 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) @@ -828,16 +823,14 @@ comments: true } /* 哨兵划分(三数取中值) */ - int partition(int[] nums, int left, int right) - { + int partition(int[] nums, int left, int right) { // 选取三个候选元素的中位数 int med = medianThree(nums, left, (left + right) / 2, right); // 将中位数交换至数组最左端 swap(nums, left, med); // 以 nums[left] 作为基准数 int i = left, j = right; - while (i < j) - { + while (i < j) { while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素 while (i < j && nums[i] <= nums[left]) @@ -1063,21 +1056,16 @@ comments: true ```csharp title="quick_sort.cs" /* 快速排序(尾递归优化) */ - void quickSort(int[] nums, int left, int right) - { + void quickSort(int[] nums, int left, int right) { // 子数组长度为 1 时终止 - while (left < right) - { + while (left < right) { // 哨兵划分操作 int pivot = partition(nums, left, right); // 对两个子数组中较短的那个执行快排 - if (pivot - left < right - pivot) - { + if (pivot - left < right - pivot) { quickSort(nums, left, pivot - 1); // 递归排序左子数组 left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right] - } - else - { + } else { quickSort(nums, pivot + 1, right); // 递归排序右子数组 right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1] } diff --git a/chapter_sorting/radix_sort.md b/chapter_sorting/radix_sort.md index a0b4dcd81..7d84f50d5 100644 --- a/chapter_sorting/radix_sort.md +++ b/chapter_sorting/radix_sort.md @@ -411,57 +411,48 @@ $$ ```csharp title="radix_sort.cs" /* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */ - int digit(int num, int exp) - { + int digit(int num, int exp) { // 传入 exp 而非 k 可以避免在此重复执行昂贵的次方计算 return (num / exp) % 10; } /* 计数排序(根据 nums 第 k 位排序) */ - void countingSortDigit(int[] nums, int exp) - { + void countingSortDigit(int[] nums, int exp) { // 十进制的位范围为 0~9 ,因此需要长度为 10 的桶 int[] counter = new int[10]; int n = nums.Length; // 统计 0~9 各数字的出现次数 - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int d = digit(nums[i], exp); // 获取 nums[i] 第 k 位,记为 d counter[d]++; // 统计数字 d 的出现次数 } // 求前缀和,将“出现个数”转换为“数组索引” - for (int i = 1; i < 10; i++) - { + for (int i = 1; i < 10; i++) { counter[i] += counter[i - 1]; } // 倒序遍历,根据桶内统计结果,将各元素填入 res int[] res = new int[n]; - for (int i = n - 1; i >= 0; i--) - { + for (int i = n - 1; i >= 0; i--) { int d = digit(nums[i], exp); int j = counter[d] - 1; // 获取 d 在数组中的索引 j res[j] = nums[i]; // 将当前元素填入索引 j counter[d]--; // 将 d 的数量减 1 } // 使用结果覆盖原数组 nums - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { nums[i] = res[i]; } } /* 基数排序 */ - void radixSort(int[] nums) - { + void radixSort(int[] nums) { // 获取数组的最大元素,用于判断最大位数 int m = int.MinValue; - foreach (int num in nums) - { + foreach (int num in nums) { if (num > m) m = num; - } + } // 按照从低位到高位的顺序遍历 - for (int exp = 1; exp <= m; exp *= 10) - { + for (int exp = 1; exp <= m; exp *= 10) { // 对数组元素的第 k 位执行计数排序 // k = 1 -> exp = 1 // k = 2 -> exp = 10 diff --git a/chapter_stack_and_queue/deque.md b/chapter_stack_and_queue/deque.md index 1af8d9348..cfd149f82 100644 --- a/chapter_stack_and_queue/deque.md +++ b/chapter_stack_and_queue/deque.md @@ -1067,7 +1067,7 @@ comments: true typedef struct linkedListDeque linkedListDeque; - /* 构造j */ + /* 构造函数 */ linkedListDeque *newLinkedListDeque() { linkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque)); deque->front = NULL; @@ -1606,14 +1606,14 @@ comments: true /* 访问队首元素 */ 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]; @@ -1812,12 +1812,14 @@ comments: true 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/chapter_stack_and_queue/queue.md b/chapter_stack_and_queue/queue.md index 84b76b72f..a8e0bf81b 100755 --- a/chapter_stack_and_queue/queue.md +++ b/chapter_stack_and_queue/queue.md @@ -328,7 +328,7 @@ comments: true /* 访问队首元素 */ public int peek() { if (size() == 0) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return front.val; } @@ -768,43 +768,35 @@ comments: true ```csharp title="linkedlist_queue.cs" /* 基于链表实现的队列 */ - class LinkedListQueue - { + class LinkedListQueue { private ListNode? front, rear; // 头节点 front ,尾节点 rear private int queSize = 0; - public LinkedListQueue() - { + public LinkedListQueue() { front = null; rear = null; } /* 获取队列的长度 */ - public int size() - { + public int size() { return queSize; } /* 判断队列是否为空 */ - public bool isEmpty() - { + public bool isEmpty() { return size() == 0; } /* 入队 */ - public void push(int num) - { + public void push(int num) { // 尾节点后添加 num ListNode node = new ListNode(num); // 如果队列为空,则令头、尾节点都指向该节点 - if (front == null) - { + if (front == null) { front = node; rear = node; // 如果队列不为空,则将该节点添加到尾节点后 - } - else if (rear != null) - { + } else if (rear != null) { rear.next = node; rear = node; } @@ -812,8 +804,7 @@ comments: true } /* 出队 */ - public int pop() - { + public int pop() { int num = peek(); // 删除头节点 front = front?.next; @@ -822,23 +813,20 @@ comments: true } /* 访问队首元素 */ - public int peek() - { + public int peek() { if (size() == 0 || front == null) throw new Exception(); return front.val; } /* 将链表转化为 Array 并返回 */ - public int[] toArray() - { + public int[] toArray() { if (front == null) return Array.Empty(); ListNode node = front; int[] res = new int[size()]; - for (int i = 0; i < res.Length; i++) - { + for (int i = 0; i < res.Length; i++) { res[i] = node.val; node = node.next; } @@ -1086,7 +1074,7 @@ comments: true /* 访问队首元素 */ public int peek() { if (isEmpty()) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return nums[front]; } @@ -1207,9 +1195,10 @@ comments: true 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 @@ -1225,7 +1214,8 @@ comments: true 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]: @@ -1537,41 +1527,34 @@ comments: true ```csharp title="array_queue.cs" /* 基于环形数组实现的队列 */ - class ArrayQueue - { + class ArrayQueue { private int[] nums; // 用于存储队列元素的数组 private int front; // 队首指针,指向队首元素 private int queSize; // 队列长度 - public ArrayQueue(int capacity) - { + public ArrayQueue(int capacity) { nums = new int[capacity]; front = queSize = 0; } /* 获取队列的容量 */ - public int capacity() - { + public int capacity() { return nums.Length; } /* 获取队列的长度 */ - public int size() - { + public int size() { return queSize; } /* 判断队列是否为空 */ - public bool isEmpty() - { + public bool isEmpty() { return queSize == 0; } /* 入队 */ - public void push(int num) - { - if (queSize == capacity()) - { + public void push(int num) { + if (queSize == capacity()) { Console.WriteLine("队列已满"); return; } @@ -1584,8 +1567,7 @@ comments: true } /* 出队 */ - public int pop() - { + public int pop() { int num = peek(); // 队首指针向后移动一位,若越过尾部则返回到数组头部 front = (front + 1) % capacity(); @@ -1594,20 +1576,17 @@ comments: true } /* 访问队首元素 */ - public int peek() - { + public int peek() { if (isEmpty()) throw new Exception(); return nums[front]; } /* 返回数组 */ - public int[] toArray() - { + public int[] toArray() { // 仅转换有效长度范围内的列表元素 int[] res = new int[queSize]; - for (int i = 0, j = front; i < queSize; i++, j++) - { + for (int i = 0, j = front; i < queSize; i++, j++) { res[i] = nums[j % this.capacity()]; } return res; diff --git a/chapter_stack_and_queue/stack.md b/chapter_stack_and_queue/stack.md index fc042619a..20773498c 100755 --- a/chapter_stack_and_queue/stack.md +++ b/chapter_stack_and_queue/stack.md @@ -320,7 +320,7 @@ comments: true /* 访问栈顶元素 */ public int peek() { if (size() == 0) - throw new EmptyStackException(); + throw new IndexOutOfBoundsException(); return stackPeek.val; } @@ -706,31 +706,26 @@ comments: true ```csharp title="linkedlist_stack.cs" /* 基于链表实现的栈 */ - class LinkedListStack - { + class LinkedListStack { private ListNode? stackPeek; // 将头节点作为栈顶 private int stkSize = 0; // 栈的长度 - public LinkedListStack() - { + public LinkedListStack() { stackPeek = null; } /* 获取栈的长度 */ - public int size() - { + public int size() { return stkSize; } /* 判断栈是否为空 */ - public bool isEmpty() - { + public bool isEmpty() { return size() == 0; } /* 入栈 */ - public void push(int num) - { + public void push(int num) { ListNode node = new ListNode(num); node.next = stackPeek; stackPeek = node; @@ -738,8 +733,7 @@ comments: true } /* 出栈 */ - public int pop() - { + public int pop() { if (stackPeek == null) throw new Exception(); @@ -750,23 +744,20 @@ comments: true } /* 访问栈顶元素 */ - public int peek() - { + public int peek() { if (size() == 0 || stackPeek == null) throw new Exception(); return stackPeek.val; } /* 将 List 转化为 Array 并返回 */ - public int[] toArray() - { + public int[] toArray() { if (stackPeek == null) return Array.Empty(); ListNode node = stackPeek; int[] res = new int[size()]; - for (int i = res.Length - 1; i >= 0; i--) - { + for (int i = res.Length - 1; i >= 0; i--) { res[i] = node.val; node = node.next; } @@ -956,14 +947,14 @@ comments: true /* 出栈 */ 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); } @@ -1042,12 +1033,14 @@ comments: true 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]: @@ -1262,36 +1255,30 @@ comments: true ```csharp title="array_stack.cs" /* 基于数组实现的栈 */ - class ArrayStack - { + class ArrayStack { private List stack; - public ArrayStack() - { + public ArrayStack() { // 初始化列表(动态数组) stack = new(); } /* 获取栈的长度 */ - public int size() - { + public int size() { return stack.Count(); } /* 判断栈是否为空 */ - public bool isEmpty() - { + public bool isEmpty() { return size() == 0; } /* 入栈 */ - public void push(int num) - { + public void push(int num) { stack.Add(num); } /* 出栈 */ - public int pop() - { + public int pop() { if (isEmpty()) throw new Exception(); var val = peek(); @@ -1300,16 +1287,14 @@ comments: true } /* 访问栈顶元素 */ - public int peek() - { + public int peek() { if (isEmpty()) throw new Exception(); return stack[size() - 1]; } /* 将 List 转化为 Array 并返回 */ - public int[] toArray() - { + public int[] toArray() { return stack.ToArray(); } } diff --git a/chapter_tree/avl_tree.md b/chapter_tree/avl_tree.md index d872a063b..5bfb2f639 100644 --- a/chapter_tree/avl_tree.md +++ b/chapter_tree/avl_tree.md @@ -315,15 +315,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit ```csharp title="avl_tree.cs" /* 获取节点高度 */ - int height(TreeNode? node) - { + int height(TreeNode? node) { // 空节点高度为 -1 ,叶节点高度为 0 return node == null ? -1 : node.height; } /* 更新节点高度 */ - void updateHeight(TreeNode node) - { + void updateHeight(TreeNode node) { // 节点高度等于最高子树高度 + 1 node.height = Math.Max(height(node.left), height(node.right)) + 1; } @@ -460,8 +458,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit ```csharp title="avl_tree.cs" /* 获取平衡因子 */ - int balanceFactor(TreeNode? node) - { + int balanceFactor(TreeNode? node) { // 空节点平衡因子为 0 if (node == null) return 0; // 节点平衡因子 = 左子树高度 - 右子树高度 @@ -657,8 +654,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 ```csharp title="avl_tree.cs" /* 右旋操作 */ - TreeNode? rightRotate(TreeNode? node) - { + TreeNode? rightRotate(TreeNode? node) { TreeNode? child = node.left; TreeNode? grandChild = child?.right; // 以 child 为原点,将 node 向右旋转 @@ -854,8 +850,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 ```csharp title="avl_tree.cs" /* 左旋操作 */ - TreeNode? leftRotate(TreeNode? node) - { + TreeNode? leftRotate(TreeNode? node) { TreeNode? child = node.right; TreeNode? grandChild = child?.left; // 以 child 为原点,将 node 向左旋转 @@ -1182,35 +1177,26 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 ```csharp title="avl_tree.cs" /* 执行旋转操作,使该子树重新恢复平衡 */ - TreeNode? rotate(TreeNode? node) - { + TreeNode? rotate(TreeNode? node) { // 获取节点 node 的平衡因子 int balanceFactorInt = balanceFactor(node); // 左偏树 - if (balanceFactorInt > 1) - { - if (balanceFactor(node.left) >= 0) - { + if (balanceFactorInt > 1) { + if (balanceFactor(node.left) >= 0) { // 右旋 return rightRotate(node); - } - else - { + } else { // 先左旋后右旋 node.left = leftRotate(node?.left); return rightRotate(node); } } // 右偏树 - if (balanceFactorInt < -1) - { - if (balanceFactor(node.right) <= 0) - { + if (balanceFactorInt < -1) { + if (balanceFactor(node.right) <= 0) { // 左旋 return leftRotate(node); - } - else - { + } else { // 先右旋后左旋 node.right = rightRotate(node?.right); return leftRotate(node); @@ -1491,14 +1477,12 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 ```csharp title="avl_tree.cs" /* 插入节点 */ - void insert(int val) - { + void insert(int val) { root = insertHelper(root, val); } /* 递归插入节点(辅助方法) */ - TreeNode? insertHelper(TreeNode? node, int val) - { + TreeNode? insertHelper(TreeNode? node, int val) { if (node == null) return new TreeNode(val); /* 1. 查找插入位置,并插入节点 */ if (val < node.val) @@ -1904,24 +1888,20 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 ```csharp title="avl_tree.cs" /* 删除节点 */ - void remove(int val) - { + void remove(int val) { root = removeHelper(root, val); } /* 递归删除节点(辅助方法) */ - TreeNode? removeHelper(TreeNode? node, int val) - { + TreeNode? removeHelper(TreeNode? node, int val) { if (node == null) return null; /* 1. 查找节点,并删除之 */ if (val < node.val) node.left = removeHelper(node.left, val); else if (val > node.val) node.right = removeHelper(node.right, val); - else - { - if (node.left == null || node.right == null) - { + else { + if (node.left == null || node.right == null) { TreeNode? child = node.left != null ? node.left : node.right; // 子节点数量 = 0 ,直接删除 node 并返回 if (child == null) @@ -1929,13 +1909,10 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉 // 子节点数量 = 1 ,直接删除 node else node = child; - } - else - { + } else { // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 TreeNode? temp = node.right; - while (temp.left != null) - { + while (temp.left != null) { temp = temp.left; } node.right = removeHelper(node.right, temp.val); diff --git a/chapter_tree/binary_search_tree.md b/chapter_tree/binary_search_tree.md index 8c708d8fe..925dd3bb6 100755 --- a/chapter_tree/binary_search_tree.md +++ b/chapter_tree/binary_search_tree.md @@ -196,12 +196,10 @@ comments: true ```csharp title="binary_search_tree.cs" /* 查找节点 */ - TreeNode? search(int num) - { + TreeNode? search(int num) { TreeNode? cur = root; // 循环查找,越过叶节点后跳出 - while (cur != null) - { + while (cur != null) { // 目标节点在 cur 的右子树中 if (cur.val < num) cur = cur.right; // 目标节点在 cur 的左子树中 @@ -501,14 +499,12 @@ comments: true ```csharp title="binary_search_tree.cs" /* 插入节点 */ - void insert(int num) - { + void insert(int num) { // 若树为空,直接提前返回 if (root == null) return; TreeNode? cur = root, pre = null; // 循环查找,越过叶节点后跳出 - while (cur != null) - { + while (cur != null) { // 找到重复节点,直接返回 if (cur.val == num) return; pre = cur; @@ -520,8 +516,7 @@ comments: true // 插入节点 val TreeNode node = new TreeNode(num); - if (pre != null) - { + if (pre != null) { if (pre.val < num) pre.right = node; else pre.left = node; } @@ -1004,14 +999,12 @@ comments: true ```csharp title="binary_search_tree.cs" /* 删除节点 */ - void remove(int num) - { + void remove(int num) { // 若树为空,直接提前返回 if (root == null) return; TreeNode? cur = root, pre = null; // 循环查找,越过叶节点后跳出 - while (cur != null) - { + while (cur != null) { // 找到待删除节点,跳出循环 if (cur.val == num) break; pre = cur; @@ -1023,27 +1016,21 @@ comments: true // 若无待删除节点,则直接返回 if (cur == null || pre == null) return; // 子节点数量 = 0 or 1 - if (cur.left == null || cur.right == null) - { + if (cur.left == null || cur.right == null) { // 当子节点数量 = 0 / 1 时, child = null / 该子节点 TreeNode? child = cur.left != null ? cur.left : cur.right; // 删除节点 cur - if (pre.left == cur) - { + if (pre.left == cur) { pre.left = child; - } - else - { + } else { pre.right = child; } } // 子节点数量 = 2 - else - { + else { // 获取中序遍历中 cur 的下一个节点 TreeNode? tmp = cur.right; - while (tmp.left != null) - { + while (tmp.left != null) { tmp = tmp.left; } // 递归删除节点 tmp diff --git a/chapter_tree/binary_tree_traversal.md b/chapter_tree/binary_tree_traversal.md index a846566d9..16f2fd05a 100755 --- a/chapter_tree/binary_tree_traversal.md +++ b/chapter_tree/binary_tree_traversal.md @@ -203,15 +203,13 @@ comments: true ```csharp title="binary_tree_bfs.cs" /* 层序遍历 */ - List levelOrder(TreeNode root) - { + List levelOrder(TreeNode root) { // 初始化队列,加入根节点 Queue queue = new(); queue.Enqueue(root); // 初始化一个列表,用于保存遍历序列 List list = new(); - while (queue.Count != 0) - { + while (queue.Count != 0) { TreeNode node = queue.Dequeue(); // 队列出队 list.Add(node.val); // 保存节点值 if (node.left != null) @@ -548,8 +546,7 @@ comments: true ```csharp title="binary_tree_dfs.cs" /* 前序遍历 */ - void preOrder(TreeNode? root) - { + void preOrder(TreeNode? root) { if (root == null) return; // 访问优先级:根节点 -> 左子树 -> 右子树 list.Add(root.val); @@ -558,8 +555,7 @@ comments: true } /* 中序遍历 */ - void inOrder(TreeNode? root) - { + void inOrder(TreeNode? root) { if (root == null) return; // 访问优先级:左子树 -> 根节点 -> 右子树 inOrder(root.left); @@ -568,8 +564,7 @@ comments: true } /* 后序遍历 */ - void postOrder(TreeNode? root) - { + void postOrder(TreeNode? root) { if (root == null) return; // 访问优先级:左子树 -> 右子树 -> 根节点 postOrder(root.left);