Merge pull request #177 from danielsss/master
Add the TypeScript code and docs for Chapter of Binary Search
This commit is contained in:
commit
25ad73020a
54
codes/typescript/chapter_searching/binary_search.ts
Normal file
54
codes/typescript/chapter_searching/binary_search.ts
Normal file
@ -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);
|
@ -646,7 +646,7 @@ $$
|
|||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
public set(key: number, val: string) {
|
public set(key: number, val: string) {
|
||||||
let index = this.hashFunc(key);
|
let index = this.hashFunc(key);
|
||||||
this.bucket[index] = new Entry(index, val);
|
this.bucket[index] = new Entry(key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
|
@ -116,17 +116,17 @@ $$
|
|||||||
=== "Go"
|
=== "Go"
|
||||||
|
|
||||||
```go title="binary_search.go"
|
```go title="binary_search.go"
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(双闭区间) */
|
||||||
func binarySearch1(nums []int, target int) int {
|
func binarySearch(nums []int, target int) int {
|
||||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||||
i, j := 0, len(nums)
|
i, j := 0, len(nums)-1
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||||
for i < j {
|
for i <= j {
|
||||||
m := (i + j) / 2 // 计算中点索引 m
|
m := (i + j) / 2 // 计算中点索引 m
|
||||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中
|
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1
|
i = m + 1
|
||||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中
|
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m-1] 中
|
||||||
j = m
|
j = m - 1
|
||||||
} else { // 找到目标元素,返回其索引
|
} else { // 找到目标元素,返回其索引
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
@ -161,7 +161,23 @@ $$
|
|||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="binary_search.ts"
|
```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"
|
=== "C"
|
||||||
@ -208,9 +224,9 @@ $$
|
|||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while (i < j) {
|
while (i < j) {
|
||||||
int m = (i + j) / 2; // 计算中点索引 m
|
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;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m;
|
j = m;
|
||||||
else // 找到目标元素,返回其索引
|
else // 找到目标元素,返回其索引
|
||||||
return m;
|
return m;
|
||||||
@ -230,9 +246,9 @@ $$
|
|||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while (i < j) {
|
while (i < j) {
|
||||||
int m = (i + j) / 2; // 计算中点索引 m
|
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;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m;
|
j = m;
|
||||||
else // 找到目标元素,返回其索引
|
else // 找到目标元素,返回其索引
|
||||||
return m;
|
return m;
|
||||||
@ -252,9 +268,9 @@ $$
|
|||||||
# 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
# 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while i < j:
|
while i < j:
|
||||||
m = (i + j) // 2 # 计算中点索引 m
|
m = (i + j) // 2 # 计算中点索引 m
|
||||||
if nums[m] < target: # 此情况说明 target 在区间 [m+1, j) 中
|
if nums[m] < target: # 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1
|
i = m + 1
|
||||||
elif nums[m] > target: # 此情况说明 target 在区间 [i, m) 中
|
elif nums[m] > target: # 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m
|
j = m
|
||||||
else: # 找到目标元素,返回其索引
|
else: # 找到目标元素,返回其索引
|
||||||
return m
|
return m
|
||||||
@ -271,9 +287,9 @@ $$
|
|||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
for i < j {
|
for i < j {
|
||||||
m := (i + j) / 2 // 计算中点索引 m
|
m := (i + j) / 2 // 计算中点索引 m
|
||||||
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j) 中
|
if nums[m] < target { // 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1
|
i = m + 1
|
||||||
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m) 中
|
} else if nums[m] > target { // 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m
|
j = m
|
||||||
} else { // 找到目标元素,返回其索引
|
} else { // 找到目标元素,返回其索引
|
||||||
return m
|
return m
|
||||||
@ -294,9 +310,9 @@ $$
|
|||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while (i < j) {
|
while (i < j) {
|
||||||
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
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;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m;
|
j = m;
|
||||||
else // 找到目标元素,返回其索引
|
else // 找到目标元素,返回其索引
|
||||||
return m;
|
return m;
|
||||||
@ -309,7 +325,23 @@ $$
|
|||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="binary_search.ts"
|
```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"
|
=== "C"
|
||||||
@ -330,9 +362,9 @@ $$
|
|||||||
while (i < j)
|
while (i < j)
|
||||||
{
|
{
|
||||||
int m = (i + j) / 2; // 计算中点索引 m
|
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;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中
|
||||||
j = m;
|
j = m;
|
||||||
else // 找到目标元素,返回其索引
|
else // 找到目标元素,返回其索引
|
||||||
return m;
|
return m;
|
||||||
@ -407,7 +439,10 @@ $$
|
|||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title=""
|
```typescript title=""
|
||||||
|
// (i + j) 有可能超出 Number 的取值范围
|
||||||
|
let m = Math.floor((i + j) / 2);
|
||||||
|
// 更换为此写法则不会越界
|
||||||
|
let m = Math.floor(i + (j - i) / 2);
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user