diff --git a/chapter_divide_and_conquer/build_binary_tree_problem.md b/chapter_divide_and_conquer/build_binary_tree_problem.md index d28850e53..595e1e434 100644 --- a/chapter_divide_and_conquer/build_binary_tree_problem.md +++ b/chapter_divide_and_conquer/build_binary_tree_problem.md @@ -433,9 +433,9 @@ status: new 每个递归函数内的前序遍历 `preorder` 和中序遍历 `inorder` 的划分结果如图 12-9 所示。 -![built_tree_overall](build_binary_tree_problem.assets/built_tree_overall.png) +![每个递归函数中的划分结果](build_binary_tree_problem.assets/built_tree_overall.png) -

图 12-9   built_tree_overall

+

图 12-9   每个递归函数中的划分结果

设树的节点数量为 $n$ ,初始化每一个节点(执行一个递归函数 `dfs()` )使用 $O(1)$ 时间。**因此总体时间复杂度为 $O(n)$** 。 diff --git a/chapter_searching/binary_search_edge.md b/chapter_searching/binary_search_edge.md index 0ffddb7ba..d7518e5b1 100644 --- a/chapter_searching/binary_search_edge.md +++ b/chapter_searching/binary_search_edge.md @@ -85,13 +85,33 @@ status: new === "JS" ```javascript title="binary_search_edge.js" - [class]{}-[func]{binarySearchLeftEdge} + /* 二分查找最左一个 target */ + function binarySearchLeftEdge(nums, target) { + // 等价于查找 target 的插入点 + const i = binarySearchInsertion(nums, target); + // 未找到 target ,返回 -1 + if (i === nums.length || nums[i] !== target) { + return -1; + } + // 找到 target ,返回索引 i + return i; + } ``` === "TS" ```typescript title="binary_search_edge.ts" - [class]{}-[func]{binarySearchLeftEdge} + /* 二分查找最左一个 target */ + function binarySearchLeftEdge(nums: Array, target: number): number { + // 等价于查找 target 的插入点 + const i = binarySearchInsertion(nums, target); + // 未找到 target ,返回 -1 + if (i === nums.length || nums[i] !== target) { + return -1; + } + // 找到 target ,返回索引 i + return i; + } ``` === "C" @@ -251,13 +271,37 @@ status: new === "JS" ```javascript title="binary_search_edge.js" - [class]{}-[func]{binarySearchRightEdge} + /* 二分查找最右一个 target */ + function binarySearchRightEdge(nums, target) { + // 转化为查找最左一个 target + 1 + const i = binarySearchInsertion(nums, target + 1); + // j 指向最右一个 target ,i 指向首个大于 target 的元素 + const j = i - 1; + // 未找到 target ,返回 -1 + if (j === -1 || nums[j] !== target) { + return -1; + } + // 找到 target ,返回索引 j + return j; + } ``` === "TS" ```typescript title="binary_search_edge.ts" - [class]{}-[func]{binarySearchRightEdge} + /* 二分查找最右一个 target */ + function binarySearchRightEdge(nums: Array, target: number): number { + // 转化为查找最左一个 target + 1 + const i = binarySearchInsertion(nums, target + 1); + // j 指向最右一个 target ,i 指向首个大于 target 的元素 + const j = i - 1; + // 未找到 target ,返回 -1 + if (j === -1 || nums[j] !== target) { + return -1; + } + // 找到 target ,返回索引 j + return j; + } ``` === "C" diff --git a/chapter_searching/binary_search_insertion.md b/chapter_searching/binary_search_insertion.md index 971c7fb14..22652b4b9 100644 --- a/chapter_searching/binary_search_insertion.md +++ b/chapter_searching/binary_search_insertion.md @@ -118,13 +118,45 @@ status: new === "JS" ```javascript title="binary_search_insertion.js" - [class]{}-[func]{binarySearchInsertionSimple} + /* 二分查找插入点(无重复元素) */ + function binarySearchInsertionSimple(nums, target) { + let i = 0, + j = nums.length - 1; // 初始化双闭区间 [0, n-1] + while (i <= j) { + const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m, 使用 Math.floor() 向下取整 + if (nums[m] < target) { + i = m + 1; // target 在区间 [m+1, j] 中 + } else if (nums[m] > target) { + j = m - 1; // target 在区间 [i, m-1] 中 + } else { + return m; // 找到 target ,返回插入点 m + } + } + // 未找到 target ,返回插入点 i + return i; + } ``` === "TS" ```typescript title="binary_search_insertion.ts" - [class]{}-[func]{binarySearchInsertionSimple} + /* 二分查找插入点(无重复元素) */ + function binarySearchInsertionSimple(nums: Array, target: number): number { + let i = 0, + j = nums.length - 1; // 初始化双闭区间 [0, n-1] + while (i <= j) { + const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m, 使用 Math.floor() 向下取整 + if (nums[m] < target) { + i = m + 1; // target 在区间 [m+1, j] 中 + } else if (nums[m] > target) { + j = m - 1; // target 在区间 [i, m-1] 中 + } else { + return m; // 找到 target ,返回插入点 m + } + } + // 未找到 target ,返回插入点 i + return i; + } ``` === "C" @@ -353,13 +385,45 @@ status: new === "JS" ```javascript title="binary_search_insertion.js" - [class]{}-[func]{binarySearchInsertion} + /* 二分查找插入点(存在重复元素) */ + function binarySearchInsertion(nums, target) { + let i = 0, + j = nums.length - 1; // 初始化双闭区间 [0, n-1] + while (i <= j) { + const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m, 使用 Math.floor() 向下取整 + if (nums[m] < target) { + i = m + 1; // target 在区间 [m+1, j] 中 + } else if (nums[m] > target) { + j = m - 1; // target 在区间 [i, m-1] 中 + } else { + j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中 + } + } + // 返回插入点 i + return i; + } ``` === "TS" ```typescript title="binary_search_insertion.ts" - [class]{}-[func]{binarySearchInsertion} + /* 二分查找插入点(存在重复元素) */ + function binarySearchInsertion(nums: Array, target: number): number { + let i = 0, + j = nums.length - 1; // 初始化双闭区间 [0, n-1] + while (i <= j) { + const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m, 使用 Math.floor() 向下取整 + if (nums[m] < target) { + i = m + 1; // target 在区间 [m+1, j] 中 + } else if (nums[m] > target) { + j = m - 1; // target 在区间 [i, m-1] 中 + } else { + j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中 + } + } + // 返回插入点 i + return i; + } ``` === "C"