mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 16:19:46 +08:00
Finetune and fix
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
package chapter_searching
|
||||
|
||||
// 二分查找最左一个 target
|
||||
/* 二分查找最左一个 target */
|
||||
func binarySearchLeftEdge(nums []int, target int) int {
|
||||
// 等价于查找 target 的插入点
|
||||
i := binarySearchInsertion(nums, target)
|
||||
@@ -16,7 +16,7 @@ func binarySearchLeftEdge(nums []int, target int) int {
|
||||
return i
|
||||
}
|
||||
|
||||
// 二分查找最右一个 target
|
||||
/* 二分查找最右一个 target */
|
||||
func binarySearchRightEdge(nums []int, target int) int {
|
||||
// 转化为查找最左一个 target + 1
|
||||
i := binarySearchInsertion(nums, target+1)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
package chapter_searching
|
||||
|
||||
// 二分查找插入点(无重复元素)
|
||||
/* 二分查找插入点(无重复元素) */
|
||||
func binarySearchInsertionSimple(nums []int, target int) int {
|
||||
// 初始化双闭区间 [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
@@ -26,7 +26,7 @@ func binarySearchInsertionSimple(nums []int, target int) int {
|
||||
return i
|
||||
}
|
||||
|
||||
// 二分查找插入点(存在重复元素)
|
||||
/* 二分查找插入点(存在重复元素) */
|
||||
func binarySearchInsertion(nums []int, target int) int {
|
||||
// 初始化双闭区间 [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
|
||||
Reference in New Issue
Block a user