mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 12:56:17 +00:00
add erfen in go
This commit is contained in:
parent
a177043d3f
commit
02aaf283e1
@ -76,25 +76,49 @@ public static int lowBoundnum(int[] nums,int target,int left, int right) {
|
|||||||
```java
|
```java
|
||||||
public static int upperBoundnum(int[] nums,int target,int left, int right) {
|
public static int upperBoundnum(int[] nums,int target,int left, int right) {
|
||||||
|
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
|
|
||||||
int mid = left + ((right - left) >> 1);
|
int mid = left + ((right - left) >> 1);
|
||||||
//小于目标值
|
//小于目标值
|
||||||
if (nums[mid] < target) {
|
if (nums[mid] < target) {
|
||||||
//看看是不是当前区间的最后一位,如果当前小于,后面一位大于,返回当前值即可
|
//看看是不是当前区间的最后一位,如果当前小于,后面一位大于,返回当前值即可
|
||||||
if (mid == right || nums[mid+1] >= target) {
|
if (mid == right || nums[mid+1] >= target) {
|
||||||
return mid;
|
return mid;
|
||||||
}
|
|
||||||
else{
|
|
||||||
left = mid + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (nums[mid] >= target){
|
|
||||||
right = mid - 1;
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user