mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-28 05:16:18 +00:00
add erfen in go
This commit is contained in:
parent
a177043d3f
commit
02aaf283e1
@ -98,3 +98,27 @@ public static int upperBoundnum(int[] nums,int target,int left, int right) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Go Code:
|
||||||
|
```go
|
||||||
|
func upperBoundnum(nums []int, target, left, right int) int {
|
||||||
|
|
||||||
|
for left <= right {
|
||||||
|
|
||||||
|
mid := left + ((right - left) >> 1)
|
||||||
|
//小于目标值
|
||||||
|
if nums[mid] < target {
|
||||||
|
//看看是不是当前区间的最后一位,如果当前小于,后面一位大于,返回当前值即可
|
||||||
|
if mid == right || nums[mid+1] >= target {
|
||||||
|
return mid
|
||||||
|
} else {
|
||||||
|
left = mid + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if nums[mid] >= target {
|
||||||
|
right = mid - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//没有查询到的情况
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user