diff --git a/codes/dart/chapter_computational_complexity/space_complexity.dart b/codes/dart/chapter_computational_complexity/space_complexity.dart index 6b5d8c1aa..099fcf711 100644 --- a/codes/dart/chapter_computational_complexity/space_complexity.dart +++ b/codes/dart/chapter_computational_complexity/space_complexity.dart @@ -20,8 +20,8 @@ void constant(int n) { // 常量、变量、对象占用 O(1) 空间 final int a = 0; int b = 0; - List nums = List.filled(10000, 0); + ListNode node = ListNode(0); // 循环中的变量占用 O(1) 空间 for (var i = 0; i < n; i++) { int c = 0; @@ -61,7 +61,6 @@ void quadratic(int n) { List> numMatrix = List.generate(n, (_) => List.filled(n, 0)); // 二维列表占用 O(n^2) 空间 List> numList = []; - for (var i = 0; i < n; i++) { List tmp = []; for (int j = 0; j < n; j++) { diff --git a/codes/dart/chapter_heap/my_heap.dart b/codes/dart/chapter_heap/my_heap.dart index 98ba282fe..23659b4bc 100644 --- a/codes/dart/chapter_heap/my_heap.dart +++ b/codes/dart/chapter_heap/my_heap.dart @@ -6,6 +6,7 @@ import '../utils/print_util.dart'; +/* 大顶堆 */ class MaxHeap { late List _maxHeap; @@ -15,7 +16,7 @@ class MaxHeap { _maxHeap = nums; // 堆化除叶节点以外的其他所有节点 for (int i = _parent(size() - 1); i >= 0; i--) { - _siftDown(i); + siftDown(i); } } @@ -61,11 +62,11 @@ class MaxHeap { // 添加节点 _maxHeap.add(val); // 从底至顶堆化 - _siftUp(size() - 1); + siftUp(size() - 1); } /* 从节点 i 开始,从底至顶堆化 */ - void _siftUp(int i) { + void siftUp(int i) { while (true) { // 获取节点 i 的父节点 int p = _parent(i); @@ -89,13 +90,13 @@ class MaxHeap { // 删除节点 int val = _maxHeap.removeLast(); // 从顶至底堆化 - _siftDown(0); + siftDown(0); // 返回堆顶元素 return val; } /* 从节点 i 开始,从顶至底堆化 */ - void _siftDown(int i) { + void siftDown(int i) { while (true) { // 判断节点 i, l, r 中值最大的节点,记为 ma int l = _left(i); diff --git a/codes/dart/chapter_heap/top_k.dart b/codes/dart/chapter_heap/top_k.dart new file mode 100644 index 000000000..9101eb9fe --- /dev/null +++ b/codes/dart/chapter_heap/top_k.dart @@ -0,0 +1,150 @@ +/** + * File: top_k.dart + * Created Time: 2023-08-15 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import '../utils/print_util.dart'; + +/* 基于堆查找数组中最大的 k 个元素 */ +MinHeap topKHeap(List nums, int k) { + // 将数组的前 k 个元素入堆 + MinHeap heap = MinHeap(nums.sublist(0, k)); + // 从第 k+1 个元素开始,保持堆的长度为 k + for (int i = k; i < nums.length; i++) { + // 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆 + if (nums[i] > heap.peek()) { + heap.pop(); + heap.push(nums[i]); + } + } + return heap; +} + +/* Driver Code */ +void main() { + List nums = [1, 7, 6, 3, 2]; + int k = 3; + + MinHeap res = topKHeap(nums, k); + print("最大的 $k 个元素为"); + res.print(); +} + +/* 小顶堆 */ +class MinHeap { + late List _minHeap; + + /* 构造方法,根据输入列表建堆 */ + MinHeap(List nums) { + // 将列表元素原封不动添加进堆 + _minHeap = nums; + // 堆化除叶节点以外的其他所有节点 + for (int i = _parent(size() - 1); i >= 0; i--) { + siftDown(i); + } + } + + /* 返回堆中的元素 */ + List getHeap() { + return _minHeap; + } + + /* 获取左子节点索引 */ + int _left(int i) { + return 2 * i + 1; + } + + /* 获取右子节点索引 */ + int _right(int i) { + return 2 * i + 2; + } + + /* 获取父节点索引 */ + int _parent(int i) { + return (i - 1) ~/ 2; // 向下整除 + } + + /* 交换元素 */ + void _swap(int i, int j) { + int tmp = _minHeap[i]; + _minHeap[i] = _minHeap[j]; + _minHeap[j] = tmp; + } + + /* 获取堆大小 */ + int size() { + return _minHeap.length; + } + + /* 判断堆是否为空 */ + bool isEmpty() { + return size() == 0; + } + + /* 访问堆顶元素 */ + int peek() { + return _minHeap[0]; + } + + /* 元素入堆 */ + void push(int val) { + // 添加节点 + _minHeap.add(val); + // 从底至顶堆化 + siftUp(size() - 1); + } + + /* 从节点 i 开始,从底至顶堆化 */ + void siftUp(int i) { + while (true) { + // 获取节点 i 的父节点 + int p = _parent(i); + // 当“越过根节点”或“节点无需修复”时,结束堆化 + if (p < 0 || _minHeap[i] >= _minHeap[p]) { + break; + } + // 交换两节点 + _swap(i, p); + // 循环向上堆化 + i = p; + } + } + + /* 元素出堆 */ + int pop() { + // 判空处理 + if (isEmpty()) throw Exception('堆为空'); + // 交换根节点与最右叶节点(即交换首元素与尾元素) + _swap(0, size() - 1); + // 删除节点 + int val = _minHeap.removeLast(); + // 从顶至底堆化 + siftDown(0); + // 返回堆顶元素 + return val; + } + + /* 从节点 i 开始,从顶至底堆化 */ + void siftDown(int i) { + while (true) { + // 判断节点 i, l, r 中值最大的节点,记为 ma + int l = _left(i); + int r = _right(i); + int mi = i; + if (l < size() && _minHeap[l] < _minHeap[mi]) mi = l; + if (r < size() && _minHeap[r] < _minHeap[mi]) mi = r; + // 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出 + if (mi == i) break; + // 交换两节点 + _swap(i, mi); + // 循环向下堆化 + i = mi; + } + } + + /* 打印堆(二叉树) */ + void print() { + printHeap(_minHeap); + } +} diff --git a/codes/dart/chapter_searching/two_sum.dart b/codes/dart/chapter_searching/two_sum.dart index 3819670ba..3dd661a24 100644 --- a/codes/dart/chapter_searching/two_sum.dart +++ b/codes/dart/chapter_searching/two_sum.dart @@ -9,6 +9,7 @@ import 'dart:collection'; /* 方法一: 暴力枚举 */ List twoSumBruteForce(List nums, int target) { int size = nums.length; + // 两层循环,时间复杂度 O(n^2) for (var i = 0; i < size - 1; i++) { for (var j = i + 1; j < size; j++) { if (nums[i] + nums[j] == target) return [i, j]; @@ -20,7 +21,9 @@ List twoSumBruteForce(List nums, int target) { /* 方法二: 辅助哈希表 */ List twoSumHashTable(List nums, int target) { int size = nums.length; + // 辅助哈希表,空间复杂度 O(n) Map dic = HashMap(); + // 单层循环,时间复杂度 O(n) for (var i = 0; i < size; i++) { if (dic.containsKey(target - nums[i])) { return [dic[target - nums[i]]!, i]; diff --git a/codes/dart/chapter_tree/array_binary_tree.dart b/codes/dart/chapter_tree/array_binary_tree.dart new file mode 100644 index 000000000..3eef40b6c --- /dev/null +++ b/codes/dart/chapter_tree/array_binary_tree.dart @@ -0,0 +1,152 @@ +/** + * File: array_binary_tree.dart + * Created Time: 2023-08-15 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import '../utils/print_util.dart'; +import '../utils/tree_node.dart'; + +/* 数组表示下的二叉树类 */ +class ArrayBinaryTree { + late List _tree; + + /* 构造方法 */ + ArrayBinaryTree(this._tree); + + /* 节点数量 */ + int size() { + return _tree.length; + } + + /* 获取索引为 i 节点的值 */ + int? val(int i) { + // 若索引越界,则返回 null ,代表空位 + if (i < 0 || i >= size()) { + return null; + } + return _tree[i]; + } + + /* 获取索引为 i 节点的左子节点的索引 */ + int? left(int i) { + return 2 * i + 1; + } + + /* 获取索引为 i 节点的右子节点的索引 */ + int? right(int i) { + return 2 * i + 2; + } + + /* 获取索引为 i 节点的父节点的索引 */ + int? parent(int i) { + return (i - 1) ~/ 2; + } + + /* 层序遍历 */ + List levelOrder() { + List res = []; + for (int i = 0; i < size(); i++) { + if (val(i) != null) { + res.add(val(i)!); + } + } + return res; + } + + /* 深度优先遍历 */ + void dfs(int i, String order, List res) { + // 若为空位,则返回 + if (val(i) == null) { + return; + } + // 前序遍历 + if (order == 'pre') { + res.add(val(i)); + } + dfs(left(i)!, order, res); + // 中序遍历 + if (order == 'in') { + res.add(val(i)); + } + dfs(right(i)!, order, res); + // 后序遍历 + if (order == 'post') { + res.add(val(i)); + } + } + + /* 前序遍历 */ + List preOrder() { + List res = []; + dfs(0, 'pre', res); + return res; + } + + /* 中序遍历 */ + List inOrder() { + List res = []; + dfs(0, 'in', res); + return res; + } + + /* 后序遍历 */ + List postOrder() { + List res = []; + dfs(0, 'post', res); + return res; + } +} + +/* Driver Code */ +void main() { + // 初始化二叉树 + // 这里借助了一个从数组直接生成二叉树的函数 + List arr = [ + 1, + 2, + 3, + 4, + null, + 6, + 7, + 8, + 9, + null, + null, + 12, + null, + null, + 15 + ]; + + TreeNode? root = listToTree(arr); + print("\n初始化二叉树\n"); + print("二叉树的数组表示:"); + print(arr); + print("二叉树的链表表示:"); + printTree(root); + + // 数组表示下的二叉树类 + ArrayBinaryTree abt = ArrayBinaryTree(arr); + + // 访问节点 + int i = 1; + int? l = abt.left(i); + int? r = abt.right(i); + int? p = abt.parent(i); + print("\n当前节点的索引为 $i ,值为 ${abt.val(i)}"); + print("其左子节点的索引为 $l ,值为 ${(l == null ? "null" : abt.val(l))}"); + print("其右子节点的索引为 $r ,值为 ${(r == null ? "null" : abt.val(r))}"); + print("其父节点的索引为 $p ,值为 ${(p == null ? "null" : abt.val(p))}"); + + // 遍历树 + List res = abt.levelOrder(); + print("\n层序遍历为:$res"); + res = abt.preOrder(); + print("前序遍历为 $res"); + res = abt.inOrder(); + print("中序遍历为 $res"); + res = abt.postOrder(); + print("后序遍历为 $res"); +} diff --git a/codes/dart/chapter_tree/avl_tree.dart b/codes/dart/chapter_tree/avl_tree.dart index 67fa1cd5a..b68096e64 100644 --- a/codes/dart/chapter_tree/avl_tree.dart +++ b/codes/dart/chapter_tree/avl_tree.dart @@ -18,6 +18,7 @@ class AVLTree { /* 获取节点高度 */ int height(TreeNode? node) { + // 空节点高度为 -1 ,叶节点高度为 0 return node == null ? -1 : node.height; } diff --git a/codes/dart/chapter_tree/binary_search_tree.dart b/codes/dart/chapter_tree/binary_search_tree.dart index fd7654058..92beedb11 100644 --- a/codes/dart/chapter_tree/binary_search_tree.dart +++ b/codes/dart/chapter_tree/binary_search_tree.dart @@ -8,122 +8,125 @@ import '../utils/print_util.dart'; import '../utils/tree_node.dart'; /* 二叉搜索树 */ -TreeNode? root; +class BinarySearchTree { + late TreeNode? _root; -void binarySearchTree(List nums) { - nums.sort(); // 排序数组 - root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树 -} - -/* 获取二叉树的根节点 */ -TreeNode? getRoot() { - return root; -} - -/* 构建二叉上搜索树 */ -TreeNode? buildTree(List nums, int i, int j) { - if (i > j) { - return null; + /* 构造方法 */ + BinarySearchTree(List nums) { + nums.sort(); // 排序数组 + _root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树 } - // 将数组中间节点作为根节点 - int mid = (i + j) ~/ 2; - TreeNode? root = TreeNode(nums[mid]); - root.left = buildTree(nums, i, mid - 1); - root.right = buildTree(nums, mid + 1, j); - return root; -} -/* 查找节点 */ -TreeNode? search(int num) { - TreeNode? cur = root; - // 循环查找,越过叶节点后跳出 - while (cur != null) { - // 目标节点在 cur 的右子树中 - if (cur.val < num) - cur = cur.right; - // 目标节点在 cur 的左子树中 - else if (cur.val > num) - cur = cur.left; - // 找到目标节点,跳出循环 + /* 获取二叉树的根节点 */ + TreeNode? getRoot() { + return _root; + } + + /* 构建二叉上搜索树 */ + TreeNode? buildTree(List nums, int i, int j) { + if (i > j) { + return null; + } + // 将数组中间节点作为根节点 + int mid = (i + j) ~/ 2; + TreeNode? root = TreeNode(nums[mid]); + root.left = buildTree(nums, i, mid - 1); + root.right = buildTree(nums, mid + 1, j); + return root; + } + + /* 查找节点 */ + TreeNode? search(int num) { + TreeNode? cur = _root; + // 循环查找,越过叶节点后跳出 + while (cur != null) { + // 目标节点在 cur 的右子树中 + if (cur.val < num) + cur = cur.right; + // 目标节点在 cur 的左子树中 + else if (cur.val > num) + cur = cur.left; + // 找到目标节点,跳出循环 + else + break; + } + // 返回目标节点 + return cur; + } + + /* 插入节点 */ + void insert(int num) { + // 若树为空,直接提前返回 + if (_root == null) return; + TreeNode? cur = _root; + TreeNode? pre = null; + // 循环查找,越过叶节点后跳出 + while (cur != null) { + // 找到重复节点,直接返回 + if (cur.val == num) return; + pre = cur; + // 插入位置在 cur 的右子树中 + if (cur.val < num) + cur = cur.right; + // 插入位置在 cur 的左子树中 + else + cur = cur.left; + } + // 插入节点 + TreeNode? node = TreeNode(num); + if (pre!.val < num) + pre.right = node; else - break; + pre.left = node; } - // 返回目标节点 - return cur; -} - -/* 插入节点 */ -void insert(int num) { - // 若树为空,直接提前返回 - if (root == null) return; - TreeNode? cur = root; - TreeNode? pre = null; - // 循环查找,越过叶节点后跳出 - while (cur != null) { - // 找到重复节点,直接返回 - if (cur.val == num) return; - pre = cur; - // 插入位置在 cur 的右子树中 - if (cur.val < num) - cur = cur.right; - // 插入位置在 cur 的左子树中 - else - cur = cur.left; - } - // 插入节点 - TreeNode? node = TreeNode(num); - if (pre!.val < num) - pre.right = node; - else - pre.left = node; -} /* 删除节点 */ -void remove(int num) { - // 若树为空,直接提前返回 - if (root == null) return; + void remove(int num) { + // 若树为空,直接提前返回 + if (_root == null) return; - TreeNode? cur = root; - TreeNode? pre = null; - // 循环查找,越过叶节点后跳出 - while (cur != null) { - // 找到待删除节点,跳出循环 - if (cur.val == num) break; - pre = cur; - // 待删除节点在 cur 的右子树中 - if (cur.val < num) - cur = cur.right; - // 待删除节点在 cur 的左子树中 - else - cur = cur.left; - } - // 若无待删除节点,直接返回 - if (cur == null) return; - // 子节点数量 = 0 or 1 - if (cur.left == null || cur.right == null) { - // 当子节点数量 = 0 / 1 时, child = null / 该子节点 - TreeNode? child = cur.left ?? cur.right; - // 删除节点 cur - if (cur != root) { - if (pre!.left == cur) - pre.left = child; + TreeNode? cur = _root; + TreeNode? pre = null; + // 循环查找,越过叶节点后跳出 + while (cur != null) { + // 找到待删除节点,跳出循环 + if (cur.val == num) break; + pre = cur; + // 待删除节点在 cur 的右子树中 + if (cur.val < num) + cur = cur.right; + // 待删除节点在 cur 的左子树中 else - pre.right = child; + cur = cur.left; + } + // 若无待删除节点,直接返回 + if (cur == null) return; + // 子节点数量 = 0 or 1 + if (cur.left == null || cur.right == null) { + // 当子节点数量 = 0 / 1 时, child = null / 该子节点 + TreeNode? child = cur.left ?? cur.right; + // 删除节点 cur + if (cur != _root) { + if (pre!.left == cur) + pre.left = child; + else + pre.right = child; + } else { + // 若删除节点为根节点,则重新指定根节点 + _root = child; + } } else { - // 若删除节点为根节点,则重新指定根节点 - root = child; + // 子节点数量 = 2 + // 获取中序遍历中 cur 的下一个节点 + TreeNode? tmp = cur.right; + while (tmp!.left != null) { + tmp = tmp.left; + } + // 递归删除节点 tmp + remove(tmp.val); + // 用 tmp 覆盖 cur + cur.val = tmp.val; } - } else { - // 子节点数量 = 2 - // 获取中序遍历中 cur 的下一个节点 - TreeNode? tmp = cur.right; - while (tmp!.left != null) { - tmp = tmp.left; - } - // 递归删除节点 tmp - remove(tmp.val); - // 用 tmp 覆盖 cur - cur.val = tmp.val; } } @@ -131,27 +134,27 @@ void remove(int num) { void main() { /* 初始化二叉搜索树 */ List nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; - binarySearchTree(nums); + BinarySearchTree bst = BinarySearchTree(nums); print("\n初始化的二叉树为\n"); - printTree(getRoot()); + printTree(bst.getRoot()); /* 查找节点 */ - TreeNode? node = search(7); + TreeNode? node = bst.search(7); print("\n查找到的节点对象为 $node,节点值 = ${node?.val}"); /* 插入节点 */ - insert(16); + bst.insert(16); print("\n插入节点 16 后,二叉树为\n"); - printTree(getRoot()); + printTree(bst.getRoot()); /* 删除节点 */ - remove(1); + bst.remove(1); print("\n删除节点 1 后,二叉树为\n"); - printTree(getRoot()); - remove(2); + printTree(bst.getRoot()); + bst.remove(2); print("\n删除节点 2 后,二叉树为\n"); - printTree(getRoot()); - remove(4); + printTree(bst.getRoot()); + bst.remove(4); print("\n删除节点 4 后,二叉树为\n"); - printTree(getRoot()); + printTree(bst.getRoot()); } diff --git a/codes/dart/utils/list_node.dart b/codes/dart/utils/list_node.dart index 2f9b522fa..703065f16 100644 --- a/codes/dart/utils/list_node.dart +++ b/codes/dart/utils/list_node.dart @@ -4,19 +4,12 @@ * Author: Jefferson (JeffersonHuang77@gmail.com) */ -/** - * Definition for a singly-linked list node - */ +/* Definition for a singly-linked list node */ class ListNode { int val; ListNode? next; ListNode(this.val, [this.next]); - - @override - String toString() { - return 'ListNode{val: $val, next: $next}'; - } } /* Generate a linked list with a vector */ diff --git a/codes/dart/utils/print_util.dart b/codes/dart/utils/print_util.dart index 1da111c5b..c91618030 100644 --- a/codes/dart/utils/print_util.dart +++ b/codes/dart/utils/print_util.dart @@ -16,6 +16,7 @@ class Trunk { Trunk(this.prev, this.str); } +/* Print a matrix (Array) */ void printMatrix(List> matrix) { print("["); for (List row in matrix) { @@ -24,6 +25,7 @@ void printMatrix(List> matrix) { print("]"); } +/* Print a linked list */ void printLinkedList(ListNode? head) { List list = []; @@ -35,6 +37,11 @@ void printLinkedList(ListNode? head) { print(list.join(' -> ')); } +/** + * The interface of the tree printer + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + */ void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) { if (root == null) { return; @@ -65,6 +72,7 @@ void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) { printTree(root.left, trunk, false); } +/* Helper function to print branches of the binary tree */ void showTrunks(Trunk? p) { if (p == null) { return; @@ -74,6 +82,7 @@ void showTrunks(Trunk? p) { stdout.write(p.str); } +/* Print a heap (PriorityQueue) */ void printHeap(List heap) { print("堆的数组表示:$heap"); print("堆的树状表示:"); diff --git a/codes/dart/utils/tree_node.dart b/codes/dart/utils/tree_node.dart index 70c09321e..dcf1fcfda 100644 --- a/codes/dart/utils/tree_node.dart +++ b/codes/dart/utils/tree_node.dart @@ -4,63 +4,47 @@ * Author: Jefferson (JeffersonHuang77@gmail.com) */ -import 'dart:collection'; - +/* 二叉树节点类 */ class TreeNode { int val; // 节点值 int height; // 节点高度 TreeNode? left; // 左子节点引用 TreeNode? right; // 右子节点引用 + /* 构造方法 */ TreeNode(this.val, [this.height = 0, this.left, this.right]); } -/** - * Generate a binary tree given an array - * @param list - * @return - */ -TreeNode? listToTree(List list) { - int size = list.length; - if (size == 0) return null; - - TreeNode root = TreeNode(list[0]); - Queue queue = Queue(); - queue.add(root); - int i = 0; - while (queue.isNotEmpty) { - TreeNode? node = queue.removeFirst(); - if (++i >= size) break; - node?.left = TreeNode(list[i]); - queue.add(node?.left); - if (++i >= size) break; - node?.right = TreeNode(list[i]); - queue.add(node?.right); +/* 将列表反序列化为二叉树:递归 */ +TreeNode? listToTreeDFS(List arr, int i) { + if (i < 0 || i >= arr.length || arr[i] == null) { + return null; } + TreeNode? root = TreeNode(arr[i]!); + root.left = listToTreeDFS(arr, 2 * i + 1); + root.right = listToTreeDFS(arr, 2 * i + 2); return root; } -/** - * Serialize a binary tree to a list - * @param root - * @return - */ -List treeToList(TreeNode? root) { - List list = []; - if (root == null) return list; - Queue queue = Queue(); - queue.add(root); - - while (!queue.isEmpty) { - TreeNode? node = queue.first; - queue.removeFirst(); - if (node != null) { - list.add(node.val); - queue.add(node.left); - queue.add(node.right); - } else { - list.add(null); - } - } - return list; +/* 将列表反序列化为二叉树 */ +TreeNode? listToTree(List arr) { + return listToTreeDFS(arr, 0); +} + +/* 将二叉树序列化为列表:递归 */ +void treeToListDFS(TreeNode? root, int i, List res) { + if (root == null) return; + while (i >= res.length) { + res.add(null); + } + res[i] = root.val; + treeToListDFS(root.left, 2 * i + 1, res); + treeToListDFS(root.right, 2 * i + 2, res); +} + +/* 将二叉树序列化为列表 */ +List treeToList(TreeNode? root) { + List res = []; + treeToListDFS(root, 0, res); + return res; } diff --git a/docs/chapter_appendix/installation.md b/docs/chapter_appendix/installation.md index 28c1c944b..23ad80fc0 100644 --- a/docs/chapter_appendix/installation.md +++ b/docs/chapter_appendix/installation.md @@ -44,6 +44,11 @@ 1. 下载并安装 [Swift](https://www.swift.org/download/)。 2. 在 VSCode 的插件市场中搜索 `swift` ,安装 [Swift for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang)。 +## Dart 环境 + +1. 下载并安装 [Dart](https://dart.dev/get-dart) 。 +2. 在 VSCode 的插件市场中搜索 `dart` ,安装 [Dart](https://marketplace.visualstudio.com/items?itemName=Dart-Code.dart-code) 。 + ## Rust 环境 1. 下载并安装 [Rust](https://www.rust-lang.org/tools/install)。 diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 918a774f5..2d1bc83c9 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -48,7 +48,7 @@ Val int // 节点值 Next *ListNode // 指向下一节点的指针(引用) } - + // NewListNode 构造函数,创建一个新的链表 func NewListNode(val int) *ListNode { return &ListNode{ @@ -139,7 +139,7 @@ pub fn ListNode(comptime T: type) type { return struct { const Self = @This(); - + val: T = 0, // 节点值 next: ?*Self = null, // 指向下一节点的指针(引用) @@ -184,7 +184,7 @@ ```java title="linked_list.java" /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ - // 初始化各个节点 + // 初始化各个节点 ListNode n0 = new ListNode(1); ListNode n1 = new ListNode(3); ListNode n2 = new ListNode(2); @@ -201,7 +201,7 @@ ```cpp title="linked_list.cpp" /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ - // 初始化各个节点 + // 初始化各个节点 ListNode* n0 = new ListNode(1); ListNode* n1 = new ListNode(3); ListNode* n2 = new ListNode(2); @@ -218,7 +218,7 @@ ```python title="linked_list.py" # 初始化链表 1 -> 3 -> 2 -> 5 -> 4 - # 初始化各个节点 + # 初始化各个节点 n0 = ListNode(1) n1 = ListNode(3) n2 = ListNode(2) @@ -286,7 +286,7 @@ ```c title="linked_list.c" /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ - // 初始化各个节点 + // 初始化各个节点 ListNode* n0 = newListNode(1); ListNode* n1 = newListNode(3); ListNode* n2 = newListNode(2); @@ -303,7 +303,7 @@ ```csharp title="linked_list.cs" /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ - // 初始化各个节点 + // 初始化各个节点 ListNode n0 = new ListNode(1); ListNode n1 = new ListNode(3); ListNode n2 = new ListNode(2); @@ -337,7 +337,7 @@ ```zig title="linked_list.zig" // 初始化链表 - // 初始化各个节点 + // 初始化各个节点 var n0 = inc.ListNode(i32){.val = 1}; var n1 = inc.ListNode(i32){.val = 3}; var n2 = inc.ListNode(i32){.val = 2}; @@ -747,7 +747,7 @@ Next *DoublyListNode // 指向后继节点的指针(引用) Prev *DoublyListNode // 指向前驱节点的指针(引用) } - + // NewDoublyListNode 初始化 func NewDoublyListNode(val int) *DoublyListNode { return &DoublyListNode{ @@ -847,7 +847,7 @@ pub fn ListNode(comptime T: type) type { return struct { const Self = @This(); - + val: T = 0, // 节点值 next: ?*Self = null, // 指向后继节点的指针(引用) prev: ?*Self = null, // 指向前驱节点的指针(引用) @@ -867,10 +867,10 @@ ```dart title="" /* 双向链表节点类 */ class ListNode { - int val; // 节点值 - ListNode next; // 指向后继节点的指针(引用) - ListNode prev; // 指向前驱节点的指针(引用) - ListNode(this.val, [this.next, this.prev]); // 构造函数 + int val; // 节点值 + ListNode next; // 指向后继节点的指针(引用) + ListNode prev; // 指向前驱节点的指针(引用) + ListNode(this.val, [this.next, this.prev]); // 构造函数 } ``` @@ -887,7 +887,7 @@ next: Option>>, // 指向后继节点的指针(引用) prev: Option>>, // 指向前驱节点的指针(引用) } - + /* 构造函数 */ impl ListNode { fn new(val: i32) -> Self { diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index eb359b4d3..fb42cf844 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -168,13 +168,13 @@ index = hash(key) % capacity === "Dart" ```dart title="simple_hash.dart" - [class]{}-[func]{add_hash} + [class]{}-[func]{addHash} - [class]{}-[func]{mul_hash} + [class]{}-[func]{mulHash} - [class]{}-[func]{xor_hash} + [class]{}-[func]{xorHash} - [class]{}-[func]{rot_hash} + [class]{}-[func]{rotHash} ``` === "Rust" @@ -313,7 +313,7 @@ $$ # 布尔量 True 的哈希值为 1 dec = 3.14159 - hash_dec = hash(dec) + hash_dec = hash(dec) # 小数 3.14159 的哈希值为 326484311674566659 str = "Hello 算法" @@ -421,23 +421,23 @@ $$ int num = 3; int hashNum = num.hashCode; // 整数 3 的哈希值为 34803 - + bool bol = true; int hashBol = bol.hashCode; // 布尔值 true 的哈希值为 1231 - + double dec = 3.14159; int hashDec = dec.hashCode; // 小数 3.14159 的哈希值为 2570631074981783 - + String str = "Hello 算法"; int hashStr = str.hashCode; // 字符串 Hello 算法 的哈希值为 468167534 - + List arr = [12836, "小哈"]; int hashArr = arr.hashCode; // 数组 [12836, 小哈] 的哈希值为 976512528 - + ListNode obj = new ListNode(0); int hashObj = obj.hashCode; // 节点对象 Instance of 'ListNode' 的哈希值为 1033450432 diff --git a/docs/chapter_heap/top_k.md b/docs/chapter_heap/top_k.md index 631ccda74..01a98f716 100644 --- a/docs/chapter_heap/top_k.md +++ b/docs/chapter_heap/top_k.md @@ -16,7 +16,7 @@ !!! tip - 当 $k = n$ 时,我们可以得到从大到小的序列,等价于「选择排序」算法。 + 当 $k = n$ 时,我们可以得到从大到小的序列,等价于「选择排序」算法。 ## 方法二:排序 @@ -129,7 +129,7 @@ === "Dart" ```dart title="top_k.dart" - [class]{}-[func]{top_k_heap} + [class]{}-[func]{topKHeap} ``` === "Rust"