add erfen in go

This commit is contained in:
lincongcong 2021-07-20 23:57:06 +08:00
parent a177043d3f
commit 02aaf283e1

View File

@ -76,25 +76,49 @@ public static int lowBoundnum(int[] nums,int target,int left, int right) {
```java
public static int upperBoundnum(int[] nums,int target,int left, int right) {
while (left <= right) {
while (left <= right) {
int 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;
int 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;
}
//没有查询到的情况
return -1;
}
```
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
}
```