mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 06:56:06 +00:00
build
This commit is contained in:
@@ -92,7 +92,27 @@ status: new
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_insertion.go"
|
||||
[class]{}-[func]{binarySearchInsertionSimple}
|
||||
/* 二分查找插入点(无重复元素) */
|
||||
func binarySearchInsertionSimple(nums []int, target int) int {
|
||||
// 初始化双闭区间 [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
for i <= j {
|
||||
// 计算中点索引 m
|
||||
m := i + (j-i)/2
|
||||
if nums[m] < target {
|
||||
// target 在区间 [m+1, j] 中
|
||||
i = m + 1
|
||||
} else if nums[m] > target {
|
||||
// target 在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
} else {
|
||||
// 找到 target ,返回插入点 m
|
||||
return m
|
||||
}
|
||||
}
|
||||
// 未找到 target ,返回插入点 i
|
||||
return i
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
@@ -307,7 +327,27 @@ status: new
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_insertion.go"
|
||||
[class]{}-[func]{binarySearchInsertion}
|
||||
/* 二分查找插入点(存在重复元素) */
|
||||
func binarySearchInsertion(nums []int, target int) int {
|
||||
// 初始化双闭区间 [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
for i <= j {
|
||||
// 计算中点索引 m
|
||||
m := i + (j-i)/2
|
||||
if nums[m] < target {
|
||||
// target 在区间 [m+1, j] 中
|
||||
i = m + 1
|
||||
} else if nums[m] > target {
|
||||
// target 在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
} else {
|
||||
// 首个小于 target 的元素在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
}
|
||||
}
|
||||
// 返回插入点 i
|
||||
return i
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
Reference in New Issue
Block a user