diff --git a/codes/typescript/chapter_searching/binary_search.ts b/codes/typescript/chapter_searching/binary_search.ts new file mode 100644 index 00000000..2a03994b --- /dev/null +++ b/codes/typescript/chapter_searching/binary_search.ts @@ -0,0 +1,54 @@ +/* +* File: binary_search.ts +* Created Time: 2022-12-27 +* Author: Daniel (better.sunjian@gmail.com) +*/ + +/* 二分查找(双闭区间) */ +const binarySearch = function (nums: number[], target: number): number { + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + let i = 0, j = nums.length - 1; + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + while (i <= j) { + const m = Math.floor(i + (j - i) / 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 m; + } + } + return -1; // 未找到目标元素,返回 -1 +} + +/* 二分查找(左闭右开) */ +const binarySearch1 = function (nums: number[], target: number): number { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + let i = 0, j = nums.length; + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + while (i < j) { + const m = Math.floor(i + (j - i) / 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 m; + } + } + return -1; // 未找到目标元素,返回 -1 +} + + +/* Driver Code */ +const target = 6; +const nums = [ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 ]; + +/* 二分查找(双闭区间) */ +let index = binarySearch(nums, target); +console.info('目标元素 6 的索引 = %d', index); + +/* 二分查找(左闭右开) */ +index = binarySearch1(nums, target); +console.info('目标元素 6 的索引 = %d', index); diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 2e2e5726..d8244d80 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -646,7 +646,7 @@ $$ /* 添加操作 */ public set(key: number, val: string) { let index = this.hashFunc(key); - this.bucket[index] = new Entry(index, val); + this.bucket[index] = new Entry(key, val); } /* 删除操作 */ diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index a74c07aa..35d6a321 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -116,17 +116,17 @@ $$ === "Go" ```go title="binary_search.go" - /* 二分查找(左闭右开) */ - func binarySearch1(nums []int, target int) int { - // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 - i, j := 0, len(nums) - // 循环,当搜索区间为空时跳出(当 i = j 时为空) - for i < j { + /* 二分查找(双闭区间) */ + func binarySearch(nums []int, target int) int { + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + i, j := 0, len(nums)-1 + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + for i <= j { m := (i + j) / 2 // 计算中点索引 m - if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中 + if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1 - } else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中 - j = m + } else if nums[m] > target { // 此情况说明 target 在区间 [i, m-1] 中 + j = m - 1 } else { // 找到目标元素,返回其索引 return m } @@ -161,7 +161,23 @@ $$ === "TypeScript" ```typescript title="binary_search.ts" - + /* 二分查找(双闭区间) */ + const binarySearch = function (nums: number[], target: number): number { + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + let i = 0, j = nums.length - 1; + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + while (i <= j) { + const m = Math.floor(i + (j - i) / 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 m; + } + } + return -1; // 未找到目标元素,返回 -1 + } ``` === "C" @@ -208,9 +224,9 @@ $$ // 循环,当搜索区间为空时跳出(当 i = j 时为空) while (i < j) { int m = (i + j) / 2; // 计算中点索引 m - if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中 j = m; else // 找到目标元素,返回其索引 return m; @@ -230,9 +246,9 @@ $$ // 循环,当搜索区间为空时跳出(当 i = j 时为空) while (i < j) { int m = (i + j) / 2; // 计算中点索引 m - if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中 j = m; else // 找到目标元素,返回其索引 return m; @@ -252,9 +268,9 @@ $$ # 循环,当搜索区间为空时跳出(当 i = j 时为空) while i < j: m = (i + j) // 2 # 计算中点索引 m - if nums[m] < target: # 此情况说明 target 在区间 [m+1, j) 中 + if nums[m] < target: # 此情况说明 target 在区间 [m+1, j] 中 i = m + 1 - elif nums[m] > target: # 此情况说明 target 在区间 [i, m) 中 + elif nums[m] > target: # 此情况说明 target 在区间 [i, m] 中 j = m else: # 找到目标元素,返回其索引 return m @@ -271,9 +287,9 @@ $$ // 循环,当搜索区间为空时跳出(当 i = j 时为空) for i < j { m := (i + j) / 2 // 计算中点索引 m - if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中 + if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1 - } else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中 + } else if nums[m] > target { // 此情况说明 target 在区间 [i, m] 中 j = m } else { // 找到目标元素,返回其索引 return m @@ -294,9 +310,9 @@ $$ // 循环,当搜索区间为空时跳出(当 i = j 时为空) while (i < j) { let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 - if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中 j = m; else // 找到目标元素,返回其索引 return m; @@ -309,7 +325,23 @@ $$ === "TypeScript" ```typescript title="binary_search.ts" - + /* 二分查找(左闭右开) */ + const binarySearch1 = function (nums: number[], target: number): number { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + let i = 0, j = nums.length; + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + while (i < j) { + const m = Math.floor(i + (j - i) / 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 m; + } + } + return -1; // 未找到目标元素,返回 -1 + } ``` === "C" @@ -330,9 +362,9 @@ $$ while (i < j) { int m = (i + j) / 2; // 计算中点索引 m - if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中 j = m; else // 找到目标元素,返回其索引 return m; @@ -407,7 +439,10 @@ $$ === "TypeScript" ```typescript title="" - + // (i + j) 有可能超出 Number 的取值范围 + let m = Math.floor((i + j) / 2); + // 更换为此写法则不会越界 + let m = Math.floor(i + (j - i) / 2); ``` === "C"