func leftBSearch(a []int, target int) int { left := 0 right := len(a) - 1 ans := -1
for left <= right { mid := left + (right - left)/2 if a[mid] == target { ans = mid // 先把存在解记录下来 right = mid - 1 // 调整区间,因为要找最左,区间往左调整 } else if a[mid] < target { left = mid + 1 } else if a[mid] > target { right = mid - 1 } } return ans }
func leftBSearch(a []int, target int) int { left := 0 right := len(a) - 1 ans := -1
for left <= right { mid := left + (right - left)/2 if a[mid] == target { ans = mid // 先把存在解记录下来 left = mid + 1 // 调整区间,因为要找最右,区间往右调整 } else if a[mid] < target { left = mid + 1 } else if a[mid] > target { right = mid - 1 } } return ans }