From 221bec3ea320a1ffa180d4dcd46292339078f2bc Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 15 Sep 2023 03:34:31 +0800 Subject: [PATCH] build --- .../build_binary_tree_problem.md | 139 +++++++++--------- chapter_hashing/hash_map.md | 33 +++++ chapter_stack_and_queue/stack.md | 25 ++++ chapter_tree/binary_search_tree.md | 2 +- 4 files changed, 127 insertions(+), 72 deletions(-) diff --git a/chapter_divide_and_conquer/build_binary_tree_problem.md b/chapter_divide_and_conquer/build_binary_tree_problem.md index d685b146c..d721c05ad 100644 --- a/chapter_divide_and_conquer/build_binary_tree_problem.md +++ b/chapter_divide_and_conquer/build_binary_tree_problem.md @@ -77,8 +77,7 @@ status: new ```python title="build_tree.py" def dfs( preorder: list[int], - inorder: list[int], - hmap: dict[int, int], + inorder_map: dict[int, int], i: int, l: int, r: int, @@ -90,19 +89,19 @@ status: new # 初始化根节点 root = TreeNode(preorder[i]) # 查询 m ,从而划分左右子树 - m = hmap[preorder[i]] + m = inorder_map[preorder[i]] # 子问题:构建左子树 - root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1) + root.left = dfs(preorder, inorder_map, i + 1, l, m - 1) # 子问题:构建右子树 - root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r) + root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r) # 返回根节点 return root def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None: """构建二叉树""" # 初始化哈希表,存储 inorder 元素到索引的映射 - hmap = {val: i for i, val in enumerate(inorder)} - root = dfs(preorder, inorder, hmap, 0, 0, len(inorder) - 1) + inorder_map = {val: i for i, val in enumerate(inorder)} + root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1) return root ``` @@ -110,18 +109,18 @@ status: new ```cpp title="build_tree.cpp" /* 构建二叉树:分治 */ - TreeNode *dfs(vector &preorder, vector &inorder, unordered_map &hmap, int i, int l, int r) { + TreeNode *dfs(vector &preorder, unordered_map &inorderMap, int i, int l, int r) { // 子树区间为空时终止 if (r - l < 0) return NULL; // 初始化根节点 TreeNode *root = new TreeNode(preorder[i]); // 查询 m ,从而划分左右子树 - int m = hmap[preorder[i]]; + int m = inorderMap[preorder[i]]; // 子问题:构建左子树 - root->left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root->left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root->right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 return root; } @@ -129,11 +128,11 @@ status: new /* 构建二叉树 */ TreeNode *buildTree(vector &preorder, vector &inorder) { // 初始化哈希表,存储 inorder 元素到索引的映射 - unordered_map hmap; + unordered_map inorderMap; for (int i = 0; i < inorder.size(); i++) { - hmap[inorder[i]] = i; + inorderMap[inorder[i]] = i; } - TreeNode *root = dfs(preorder, inorder, hmap, 0, 0, inorder.size() - 1); + TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1); return root; } ``` @@ -142,18 +141,18 @@ status: new ```java title="build_tree.java" /* 构建二叉树:分治 */ - TreeNode dfs(int[] preorder, int[] inorder, Map hmap, int i, int l, int r) { + TreeNode dfs(int[] preorder, Map inorderMap, int i, int l, int r) { // 子树区间为空时终止 if (r - l < 0) return null; // 初始化根节点 TreeNode root = new TreeNode(preorder[i]); // 查询 m ,从而划分左右子树 - int m = hmap.get(preorder[i]); + int m = inorderMap.get(preorder[i]); // 子问题:构建左子树 - root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root.left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 return root; } @@ -161,11 +160,11 @@ status: new /* 构建二叉树 */ TreeNode buildTree(int[] preorder, int[] inorder) { // 初始化哈希表,存储 inorder 元素到索引的映射 - Map hmap = new HashMap<>(); + Map inorderMap = new HashMap<>(); for (int i = 0; i < inorder.length; i++) { - hmap.put(inorder[i], i); + inorderMap.put(inorder[i], i); } - TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); + TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1); return root; } ``` @@ -174,18 +173,18 @@ status: new ```csharp title="build_tree.cs" /* 构建二叉树:分治 */ - TreeNode dfs(int[] preorder, int[] inorder, Dictionary hmap, int i, int l, int r) { + TreeNode dfs(int[] preorder, Dictionary inorderMap, int i, int l, int r) { // 子树区间为空时终止 if (r - l < 0) return null; // 初始化根节点 TreeNode root = new TreeNode(preorder[i]); // 查询 m ,从而划分左右子树 - int m = hmap[preorder[i]]; + int m = inorderMap[preorder[i]]; // 子问题:构建左子树 - root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root.left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 return root; } @@ -193,11 +192,11 @@ status: new /* 构建二叉树 */ TreeNode buildTree(int[] preorder, int[] inorder) { // 初始化哈希表,存储 inorder 元素到索引的映射 - Dictionary hmap = new Dictionary(); + Dictionary inorderMap = new Dictionary(); for (int i = 0; i < inorder.Length; i++) { - hmap.TryAdd(inorder[i], i); + inorderMap.TryAdd(inorder[i], i); } - TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.Length - 1); + TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.Length - 1); return root; } ``` @@ -206,7 +205,7 @@ status: new ```go title="build_tree.go" /* 构建二叉树:分治 */ - func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeNode { + func dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode { // 子树区间为空时终止 if r-l < 0 { return nil @@ -214,11 +213,11 @@ status: new // 初始化根节点 root := NewTreeNode(preorder[i]) // 查询 m ,从而划分左右子树 - m := hmap[preorder[i]] + m := inorderMap[preorder[i]] // 子问题:构建左子树 - root.Left = dfsBuildTree(preorder, inorder, hmap, i+1, l, m-1) + root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1) // 子问题:构建右子树 - root.Right = dfsBuildTree(preorder, inorder, hmap, i+1+m-l, m+1, r) + root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r) // 返回根节点 return root } @@ -226,12 +225,12 @@ status: new /* 构建二叉树 */ func buildTree(preorder, inorder []int) *TreeNode { // 初始化哈希表,存储 inorder 元素到索引的映射 - hmap := make(map[int]int, len(inorder)) + inorderMap := make(map[int]int, len(inorder)) for i := 0; i < len(inorder); i++ { - hmap[inorder[i]] = i + inorderMap[inorder[i]] = i } - root := dfsBuildTree(preorder, inorder, hmap, 0, 0, len(inorder)-1) + root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1) return root } ``` @@ -240,7 +239,7 @@ status: new ```swift title="build_tree.swift" /* 构建二叉树:分治 */ - func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? { + func dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? { // 子树区间为空时终止 if r - l < 0 { return nil @@ -248,11 +247,11 @@ status: new // 初始化根节点 let root = TreeNode(x: preorder[i]) // 查询 m ,从而划分左右子树 - let m = hmap[preorder[i]]! + let m = inorderMap[preorder[i]]! // 子问题:构建左子树 - root.left = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1, l: l, r: m - 1) + root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1) // 子问题:构建右子树 - root.right = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1 + m - l, l: m + 1, r: r) + root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r) // 返回根节点 return root } @@ -260,8 +259,8 @@ status: new /* 构建二叉树 */ func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? { // 初始化哈希表,存储 inorder 元素到索引的映射 - let hmap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset } - return dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: 0, l: 0, r: inorder.count - 1) + let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset } + return dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1) } ``` @@ -269,17 +268,17 @@ status: new ```javascript title="build_tree.js" /* 构建二叉树:分治 */ - function dfs(preorder, inorder, hmap, i, l, r) { + function dfs(preorder, inorderMap, i, l, r) { // 子树区间为空时终止 if (r - l < 0) return null; // 初始化根节点 const root = new TreeNode(preorder[i]); // 查询 m ,从而划分左右子树 - const m = hmap.get(preorder[i]); + const m = inorderMap.get(preorder[i]); // 子问题:构建左子树 - root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root.left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 return root; } @@ -287,11 +286,11 @@ status: new /* 构建二叉树 */ function buildTree(preorder, inorder) { // 初始化哈希表,存储 inorder 元素到索引的映射 - let hmap = new Map(); + let inorderMap = new Map(); for (let i = 0; i < inorder.length; i++) { - hmap.set(inorder[i], i); + inorderMap.set(inorder[i], i); } - const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); + const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1); return root; } ``` @@ -302,8 +301,7 @@ status: new /* 构建二叉树:分治 */ function dfs( preorder: number[], - inorder: number[], - hmap: Map, + inorderMap: Map, i: number, l: number, r: number @@ -313,11 +311,11 @@ status: new // 初始化根节点 const root: TreeNode = new TreeNode(preorder[i]); // 查询 m ,从而划分左右子树 - const m = hmap.get(preorder[i]); + const m = inorderMap.get(preorder[i]); // 子问题:构建左子树 - root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root.left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 return root; } @@ -325,11 +323,11 @@ status: new /* 构建二叉树 */ function buildTree(preorder: number[], inorder: number[]): TreeNode | null { // 初始化哈希表,存储 inorder 元素到索引的映射 - let hmap = new Map(); + let inorderMap = new Map(); for (let i = 0; i < inorder.length; i++) { - hmap.set(inorder[i], i); + inorderMap.set(inorder[i], i); } - const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); + const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1); return root; } ``` @@ -340,8 +338,7 @@ status: new /* 构建二叉树:分治 */ TreeNode? dfs( List preorder, - List inorder, - Map hmap, + Map inorderMap, int i, int l, int r, @@ -353,11 +350,11 @@ status: new // 初始化根节点 TreeNode? root = TreeNode(preorder[i]); // 查询 m ,从而划分左右子树 - int m = hmap[preorder[i]]!; + int m = inorderMap[preorder[i]]!; // 子问题:构建左子树 - root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root.left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 return root; } @@ -365,11 +362,11 @@ status: new /* 构建二叉树 */ TreeNode? buildTree(List preorder, List inorder) { // 初始化哈希表,存储 inorder 元素到索引的映射 - Map hmap = {}; + Map inorderMap = {}; for (int i = 0; i < inorder.length; i++) { - hmap[inorder[i]] = i; + inorderMap[inorder[i]] = i; } - TreeNode? root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); + TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1); return root; } ``` @@ -378,17 +375,17 @@ status: new ```rust title="build_tree.rs" /* 构建二叉树:分治 */ - fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap, i: i32, l: i32, r: i32) -> Option>> { + fn dfs(preorder: &[i32], inorderMap: &HashMap, i: i32, l: i32, r: i32) -> Option>> { // 子树区间为空时终止 if r - l < 0 { return None; } // 初始化根节点 let root = TreeNode::new(preorder[i as usize]); // 查询 m ,从而划分左右子树 - let m = hmap.get(&preorder[i as usize]).unwrap(); + let m = inorderMap.get(&preorder[i as usize]).unwrap(); // 子问题:构建左子树 - root.borrow_mut().left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); + root.borrow_mut().left = dfs(preorder, inorderMap, i + 1, l, m - 1); // 子问题:构建右子树 - root.borrow_mut().right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); + root.borrow_mut().right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r); // 返回根节点 Some(root) } @@ -396,11 +393,11 @@ status: new /* 构建二叉树 */ fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option>> { // 初始化哈希表,存储 inorder 元素到索引的映射 - let mut hmap: HashMap = HashMap::new(); + let mut inorderMap: HashMap = HashMap::new(); for i in 0..inorder.len() { - hmap.insert(inorder[i], i as i32); + inorderMap.insert(inorder[i], i as i32); } - let root = dfs(preorder, inorder, &hmap, 0, 0, inorder.len() as i32 - 1); + let root = dfs(preorder, &inorderMap, 0, 0, inorder.len() as i32 - 1); root } ``` diff --git a/chapter_hashing/hash_map.md b/chapter_hashing/hash_map.md index 50b682c68..011b1540d 100755 --- a/chapter_hashing/hash_map.md +++ b/chapter_hashing/hash_map.md @@ -249,7 +249,26 @@ comments: true === "Rust" ```rust title="hash_map.rs" + use std::collections::HashMap; + + /* 初始化哈希表 */ + let mut map: HashMap = HashMap::new(); + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.insert(12836, "小哈".to_string()); + map.insert(15937, "小啰".to_string()); + map.insert(16750, "小算".to_string()); + map.insert(13279, "小法".to_string()); + map.insert(10583, "小鸭".to_string()); + /* 查询操作 */ + // 向哈希表中输入键 key ,得到值 value + let _name: Option<&String> = map.get(&15937); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + let _removed_value: Option = map.remove(&10583); ``` === "C" @@ -430,7 +449,21 @@ comments: true === "Rust" ```rust title="hash_map.rs" + /* 遍历哈希表 */ + // 遍历键值对 Key->Value + for (key, value) in &map { + println!("{key} -> {value}"); + } + + // 单独遍历键 Key + for key in map.keys() { + println!("{key}"); + } + // 单独遍历值 Value + for value in map.values() { + println!("{value}"); + } ``` === "C" diff --git a/chapter_stack_and_queue/stack.md b/chapter_stack_and_queue/stack.md index cd1c387af..30343fa01 100755 --- a/chapter_stack_and_queue/stack.md +++ b/chapter_stack_and_queue/stack.md @@ -276,7 +276,32 @@ comments: true === "Rust" ```rust title="stack.rs" + /* 初始化栈 */ + // 把 Vec 当作栈来使用 + let mut stack: Vec = Vec::new(); + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + + /* 访问栈顶元素 */ + if let Some(top) = stack.get(stack.len() - 1) { + } + if let Some(top) = stack.last() { + } + + /* 元素出栈 */ + if let Some(pop) = stack.pop() { + } + + /* 获取栈的长度 */ + let size = stack.len(); + + /* 判断是否为空 */ + let isEmpty = stack.is_empty(); ``` === "C" diff --git a/chapter_tree/binary_search_tree.md b/chapter_tree/binary_search_tree.md index 59b9ba525..cb8343e6b 100755 --- a/chapter_tree/binary_search_tree.md +++ b/chapter_tree/binary_search_tree.md @@ -972,7 +972,7 @@ comments: true cur = cur.left; } // 若无待删除节点,则直接返回 - if (cur == null || pre == null) + if (cur == null) return; // 子节点数量 = 0 or 1 if (cur.left == null || cur.right == null) {