diff --git a/codes/cpp/chapter_divide_and_conquer/build_tree.cpp b/codes/cpp/chapter_divide_and_conquer/build_tree.cpp index 51b8fbe24..d35009d90 100644 --- a/codes/cpp/chapter_divide_and_conquer/build_tree.cpp +++ b/codes/cpp/chapter_divide_and_conquer/build_tree.cpp @@ -7,18 +7,18 @@ #include "../utils/common.hpp" /* 构建二叉树:分治 */ -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; } @@ -26,11 +26,11 @@ TreeNode *dfs(vector &preorder, vector &inorder, unordered_map &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; } diff --git a/codes/csharp/chapter_divide_and_conquer/build_tree.cs b/codes/csharp/chapter_divide_and_conquer/build_tree.cs index 104a4b55b..dcd091f0a 100644 --- a/codes/csharp/chapter_divide_and_conquer/build_tree.cs +++ b/codes/csharp/chapter_divide_and_conquer/build_tree.cs @@ -8,18 +8,18 @@ namespace hello_algo.chapter_divide_and_conquer; public class build_tree { /* 构建二叉树:分治 */ - public TreeNode dfs(int[] preorder, int[] inorder, Dictionary hmap, int i, int l, int r) { + public 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; } @@ -27,11 +27,11 @@ public class build_tree { /* 构建二叉树 */ public 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; } diff --git a/codes/csharp/chapter_tree/binary_search_tree.cs b/codes/csharp/chapter_tree/binary_search_tree.cs index a88dd3bf3..2b85da325 100644 --- a/codes/csharp/chapter_tree/binary_search_tree.cs +++ b/codes/csharp/chapter_tree/binary_search_tree.cs @@ -91,7 +91,7 @@ class BinarySearchTree { cur = cur.left; } // 若无待删除节点,则直接返回 - if (cur == null || pre == null) + if (cur == null) return; // 子节点数量 = 0 or 1 if (cur.left == null || cur.right == null) { diff --git a/codes/dart/chapter_divide_and_conquer/build_tree.dart b/codes/dart/chapter_divide_and_conquer/build_tree.dart index 5c7c0662c..f31cd0906 100644 --- a/codes/dart/chapter_divide_and_conquer/build_tree.dart +++ b/codes/dart/chapter_divide_and_conquer/build_tree.dart @@ -10,8 +10,7 @@ import '../utils/tree_node.dart'; /* 构建二叉树:分治 */ TreeNode? dfs( List preorder, - List inorder, - Map hmap, + Map inorderMap, int i, int l, int r, @@ -23,11 +22,11 @@ TreeNode? dfs( // 初始化根节点 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; } @@ -35,11 +34,11 @@ TreeNode? dfs( /* 构建二叉树 */ 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; } diff --git a/codes/go/chapter_divide_and_conquer/build_tree.go b/codes/go/chapter_divide_and_conquer/build_tree.go index 6e8818052..625040f7e 100644 --- a/codes/go/chapter_divide_and_conquer/build_tree.go +++ b/codes/go/chapter_divide_and_conquer/build_tree.go @@ -7,7 +7,7 @@ package chapter_divide_and_conquer import . "github.com/krahets/hello-algo/pkg" /* 构建二叉树:分治 */ -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 @@ -15,11 +15,11 @@ func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeN // 初始化根节点 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 } @@ -27,11 +27,11 @@ func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeN /* 构建二叉树 */ 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 } diff --git a/codes/java/chapter_divide_and_conquer/build_tree.java b/codes/java/chapter_divide_and_conquer/build_tree.java index 082ff5f6c..ca1085db7 100644 --- a/codes/java/chapter_divide_and_conquer/build_tree.java +++ b/codes/java/chapter_divide_and_conquer/build_tree.java @@ -11,18 +11,18 @@ import java.util.*; public class build_tree { /* 构建二叉树:分治 */ - static TreeNode dfs(int[] preorder, int[] inorder, Map hmap, int i, int l, int r) { + static 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; } @@ -30,11 +30,11 @@ public class build_tree { /* 构建二叉树 */ static 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; } diff --git a/codes/javascript/chapter_divide_and_conquer/build_tree.js b/codes/javascript/chapter_divide_and_conquer/build_tree.js index 67100bf58..1505b0495 100644 --- a/codes/javascript/chapter_divide_and_conquer/build_tree.js +++ b/codes/javascript/chapter_divide_and_conquer/build_tree.js @@ -8,17 +8,17 @@ const { printTree } = require('../modules/PrintUtil'); const { TreeNode } = require('../modules/TreeNode'); /* 构建二叉树:分治 */ -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; } @@ -26,11 +26,11 @@ function dfs(preorder, inorder, hmap, i, l, r) { /* 构建二叉树 */ 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; } diff --git a/codes/python/chapter_divide_and_conquer/build_tree.py b/codes/python/chapter_divide_and_conquer/build_tree.py index b8b2db293..970f1d4a4 100644 --- a/codes/python/chapter_divide_and_conquer/build_tree.py +++ b/codes/python/chapter_divide_and_conquer/build_tree.py @@ -12,8 +12,7 @@ from modules import * def dfs( preorder: list[int], - inorder: list[int], - hmap: dict[int, int], + inorder_map: dict[int, int], i: int, l: int, r: int, @@ -25,11 +24,11 @@ def dfs( # 初始化根节点 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 @@ -37,8 +36,8 @@ def dfs( 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 diff --git a/codes/rust/chapter_divide_and_conquer/build_tree.rs b/codes/rust/chapter_divide_and_conquer/build_tree.rs index 3478e3ab6..76f987764 100644 --- a/codes/rust/chapter_divide_and_conquer/build_tree.rs +++ b/codes/rust/chapter_divide_and_conquer/build_tree.rs @@ -10,17 +10,17 @@ include!("../include/include.rs"); use tree_node::TreeNode; /* 构建二叉树:分治 */ -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) } @@ -28,11 +28,11 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap, i: i32, l: i /* 构建二叉树 */ 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/codes/swift/chapter_divide_and_conquer/build_tree.swift b/codes/swift/chapter_divide_and_conquer/build_tree.swift index 048ffab1a..bd94ba467 100644 --- a/codes/swift/chapter_divide_and_conquer/build_tree.swift +++ b/codes/swift/chapter_divide_and_conquer/build_tree.swift @@ -7,7 +7,7 @@ import utils /* 构建二叉树:分治 */ -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 @@ -15,11 +15,11 @@ func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: I // 初始化根节点 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 } @@ -27,8 +27,8 @@ func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: I /* 构建二叉树 */ 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) } @main diff --git a/codes/typescript/chapter_divide_and_conquer/build_tree.ts b/codes/typescript/chapter_divide_and_conquer/build_tree.ts index 69f4afa04..d47b95450 100644 --- a/codes/typescript/chapter_divide_and_conquer/build_tree.ts +++ b/codes/typescript/chapter_divide_and_conquer/build_tree.ts @@ -10,8 +10,7 @@ import { TreeNode } from '../modules/TreeNode'; /* 构建二叉树:分治 */ function dfs( preorder: number[], - inorder: number[], - hmap: Map, + inorderMap: Map, i: number, l: number, r: number @@ -21,11 +20,11 @@ function dfs( // 初始化根节点 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; } @@ -33,11 +32,11 @@ function dfs( /* 构建二叉树 */ 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; } diff --git a/docs/chapter_dynamic_programming/edit_distance_problem.assets/edit_distance_dp_step8.png b/docs/chapter_dynamic_programming/edit_distance_problem.assets/edit_distance_dp_step8.png index 4d7b43a05..c9620a9d4 100644 Binary files a/docs/chapter_dynamic_programming/edit_distance_problem.assets/edit_distance_dp_step8.png and b/docs/chapter_dynamic_programming/edit_distance_problem.assets/edit_distance_dp_step8.png differ diff --git a/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_hashtable_step2.png b/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_hashtable_step2.png index 18301ac6f..c76f66956 100644 Binary files a/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_hashtable_step2.png and b/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_hashtable_step2.png differ